My goal is to send the client the HTML page (create.html) in response to a GET request triggered by a button click using fetch. I am intentionally avoiding the use of a form due to formatting and potential scalability issues. The code acknowledges that the request is sent, received, and responded to with the file, but it does not reload the page with it. Even *res.redirect does not work as expected. Here's the code snippet below. JavaScript:
app.get('/', function(req, res) {
console.log(`[00]: Get request received at '/'`);
res.sendFile('public/start.html' , { root : __dirname});
})
app.get('/login', function(req, res) {
console.log(`[01]: Get request received at '/login'`);
res.sendFile('public/login.html' , { root : __dirname});
})
app.get('/create', function(req, res) {
console.log(`[02]: Get request received at '/create'`);
res.sendFile('public/create.html' , { root : __dirname});
})
HTML:
<html>
<head>
<meta charset="utf-8">
<title>HOME PAGE</title>
</head>
<body>
<h1 id='title'>Welcome User!</h1>
<h2>Select an option below!</h2>
<button id="btnToLogin">Login</button>
<button id="btnToCreate">Create Account</button>
<p>-ADMIN PANEL-</p>
<button id="btnDisplay">Display Database</button>
<button id="btnTruncate">Truncate Database</button>
<p id='displayText' >[displayText]: Nothing seems to be here...</p>
<script src="start.js"></script>
</body>
</html>
HTML JavaScript:
// Accesses elements from start.html
var btnToLogin = document.getElementById('btnToLogin');
var btnToCreate = document.getElementById('btnToCreate');
var btnDisplay = document.getElementById('btnDisplay');
var btnTruncate = document.getElementById('btnTruncate');
var displayText = document.getElementById('displayText');
btnToLogin.addEventListener('click', function() { fetch('/login', { method: 'GET' }) });
btnToCreate.addEventListener('click', function() { fetch('/create', { method: 'GET'}) });
I have omitted most of my code, focusing on the problem at hand. All dependencies are correctly imported and the server is properly configured. Below is a screenshot of the file structure, in case it is relevant. Thank you. File Structure