Reputation: 33
I have a form and I'm inserting information into the database. That part works perfectly fine but how do I redirect to another page after that?
app.post('/create', function (req, res) {
Room.findOne({'name' :req.body.name}, function(err,room){
if(err){
return done(err);
}
if(room){
return done(null,false,req.flash('this room name is taken'));
}else{
var newRoom = new Room();
newRoom.name = req.body.name;
newRoom.topic = req.body.topic;
newRoom.participants = req.body.participants;
newRoom.type = req.body.type;
}
newRoom.save(function(err){
if (err){
throw err;
}
redirect: '/home';
})
Upvotes: 0
Views: 2732
Reputation: 158
Reference here to review http.Response API for nodejs. response.writeHead(statusCode[, statusMessage][, headers])
replace this line
redirect: '/home';
with the following
res.writeHead(302,{
'Location':'/path',
});
res.end();
The status code for redirecting is 3xx level, 302 in the example is for 'found' The 'Location' header will give the path to redirect to
If written in Express, reference here and use
res.redirect([statusCode,] '/path')
if optional status code is not explicitly expressed, it will be 302 'found' by default
Upvotes: 2
Reputation: 5603
res.redirect(pathnam)
by default it will sends the 302 status code, or you can pass the status code as the first parameter
res.redirect(301, pathname)
Upvotes: 0
Reputation: 1874
With Express you can use res.redirect()
method. For full documentation click here.
In your case, replace:
redirect: '/home';
With the following:
res.redirect(301, '/home');
Upvotes: 2