skip to Main Content

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


  1. So the message is Cannot GET /login

    But in the code I see that you define a route to /login with POST HTTP method in the first line

    app.post('/login', function (req, res) {
    

    Seems the problem is on the client side, they should use the POST method

    <form method="POST" ... >
      ...
    </form>
    
    Login or Signup to reply.
  2. The 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search