I am trying to send a new html to the user if they succesfully login. Whenever I try to send a file I always see "Cannot GET /login" on the page.
Here is the piece of code giving me trouble:
app.post('/login', function (req, res) {
var acc = {};
var data = req.body
acc.username = data.username
acc.password = data.password
fs.readFile('storage.json', (err, jsonData) => {
if (err) throw err;
let acc = JSON.parse(jsonData);
let authenticated = false;
for(var i=0; i<acc.accs.length; i++){
if(acc.accs[i].username == acc.username && acc.accs[i].password == acc.password){
authenticated = true;
break;
}
}
if(authenticated){
res.sendFile(path.join(__dirname, '/public/homepage.html'))
} else {
res.send("fail")
}
});
});
There is no problem with the HTML form, I’ve tried changing that already.
Apologies if this is a simple question with a simple answer, I am new to node/express development.
2
Answers
So the message is
Cannot GET /login
But in the code I see that you define a route to
/login
withPOST
HTTP method in the first lineSeems the problem is on the client side, they should use the
POST
methodThe issue might be that after successful login, your homepage.html might have some resources (like CSS or JS files) which are not correctly linked. When browser tries to get those resources, it might be hitting the "/login" route with a GET request, hence the error.