Omorhefere imoloame Omorhefere imoloame - 1 year ago 50
Node.js Question

How can I redirect to another page after I post data to my database?

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?'/create', function (req, res) {
Room.findOne({'name'}, function(err,room){
return done(err);

return done(null,false,req.flash('this room name is taken'));
var newRoom = new Room(); =;
newRoom.topic = req.body.topic;
newRoom.participants = req.body.participants;
newRoom.type = req.body.type;
if (err){
throw err;
redirect: '/home';

Answer Source

Reference here to review http.Response API for nodejs. response.writeHead(statusCode[, statusMessage][, headers])

replace this line

redirect: '/home';

with the following


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