Jason
Jason

Reputation: 694

Failing to get POST params using node.js and express

I am quite new to node.js and I love it. I am just trying to get a very simple login to work. I don't have a problem when I try to get the form input data useing GET. But I am sending passwords so I don't want to use GET because of the info in the url.

The form opens /login (admin.login) but I keep on getting a error

Cannot POST /login

The form is written in Jade what should not be a problem due to that a ends up in HTML anyway.

extends layout

block content 
h1 Admin Login
form(action="/login", method="post")#inputFields
    span Username
    input(type="text", name="username")
    span Password
    input(type="password", name="password")
    input(type="submit")

this is the file what the login links to

/*
 * GET home page.
 */

exports.index = function(req, res){ 
    if (req.session.UserLogin != true) {
        res.redirect("../login");
    } 

    res.render('index', { title: 'Express' });
};


exports.login = function(req, res){  
    req.
  res.render('login', { title: 'Express' });
};

Really that is all there is because it does not let me do anything after that.

Thanks a lot Jason Hornsby

Upvotes: 0

Views: 670

Answers (1)

Nick Mitchinson
Nick Mitchinson

Reputation: 5480

The problem seems to be that you don't have a route set up to handle POST request to login. You seem to be using Express as a framework, so just add a:

app.post('/login', handleLogin); 

to handle the request. You can then have a function to process:

exports.handleLogin = function (req, res) {
    //Handle request here
    var username = req.body.username;
    var password = req.body.password;
}

Note that post parameters can be accessed through the req.body object

Upvotes: 2

Related Questions