Reputation: 69
I have a node.js/express/mysql/angular.js application and I'm trying to redirect a user after they login, on the server side. This is the server-side controller and I cannot figure out how to redirect the user to another page. Ive tried res.render, res.redirect, window.location.href with no success. Errors I'm getting with res.render() are 'No default engine was specified and no extension was provided.' window.location.href says window is undefined. And res.redirect() allows me to console.log the html of the page I want to direct too. Any help is greatly appreciated, Thanks!
var Query = require('./../models/query.js');
module.exports = (function(){
return {
show: function(req,res) {
req.getConnection(function(err,connection){
connection.query('SELECT * FROM users',function(err,rows){
res.json(rows);
});
});
},
add: function(req,res){
var newUser = {
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
password: req.body.password,
created_at: req.body.created_at
};
var table = "users";
Query.insert(req,newUser,table,function(err,results){
if(err) return res.json(err);
Query.find(req,table,function(err,results){
if(err) return res.json(err);
res.json(results);
});
});
},
login: function(req,res) {
var input = req.body;
console.log("got here", input);
req.getConnection(function(req, connection) {
// console.log("got connections",connection);
connection.query('SELECT * FROM users WHERE email = ?',input.email, function(err, rows) {
if (err) {
console.log("User doesn't exist: %s", err);
res.redirect('/', { data:"failed"});
} else {
if (input.password == rows[0].password) {
console.log("User password is matched");
*** success redirect right here ****
res.redirect('/static/taxonomer'+'.html');
} else {
console.log("failed here", rows[0].password);
res.redirect( '/', { data:"failed"});
}
}
});
});
}
};
})();
Upvotes: 2
Views: 2369
Reputation: 511
Without seeing your routes I am guessing that login method on the above controller corresponds to a POST route. For example,
app.post('some/login/route', theAboveController.login)
Therefore, the redirect is controlled on the client side.
So, make then following changes:
1) Use res.send
to send a redirect url as a string to the client. For example,
res.send('/static/taxonomer'+'.html')
2) Then on the client side in your success callback, change the location to the url you received from the res.send method. For example,
$http.post('/some/login/route', data).then(function(response) {
$window.location.href = response;
}, errorCallback);
Upvotes: 3
Reputation: 8515
Express's docs are pretty good here: http://expressjs.com/api.html#res.redirect
res.redirect('https://stackoverflow.com');
will redirect them to that specific URL.
res.redirect('/foo');
will redirect to a specific URL
res.redirect('back');
will redirect to the referrer. So you can do something like a middleware that redirects to /login
and then the login finishes successfully and it goes back to the original path.
Upvotes: 1