Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

res.redirect() not working for me in node.js

I am trying to POST a request for /draft and create a new "draft" / update an existing one in my database. after that I want to instantly redirect to the /draft?id=RELEVANT_ID_HERE page.

this is my current POST request function:

app.post('/draft', function(req,res){
var id = req.query.id;
var postTitle = req.body.head;
var article = req.body.article;

if(id){
    db.postCatalog.findOneAndUpdate({_id: id}, {title:postTitle, inShort:article.substring(0,100), content:article}, function(err, data){
        if (err) {
            return handleError(err);
        }
        else {
            console.log(data);
            res.status(200).redirect('/draft?id='+id);
        }
    });
}
else{
    id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16);
    });

    new db.postCatalog({title:postTitle, 
                    _id:id, 
                    author:'temp', 
                    AuthorID:2, 
                    date:'2/3/12', 
                    inShort:article.substring(0,100), 
                    content:article ,
                    published:false
                }).save(function (err, data) {
                    if (err) {
                        return handleError(err);
                    }
                    else {
                        console.log(data);
                        res.status(200).redirect('/draft?id='+id);
                    }
                });
}
});

so, everything works except for the redirect. I am getting the correct GET request in the node console, but nothing happens in the browser. node console log

this is the code for the GET request:

app.get('/draft', function(req,res){
var id = req.query.id;
if(id){
    db.postCatalog.findOne({_id: id}, function(err, post){
        if(err) {
            return handleError(err);
        }
        else{
            if(post){
                console.log(post);
                res.status(200).render('editDraft.hbs', {post: post}); 
            }
            else{
                routes._404(req,res);
            }
        }   
    });
}
else{
    console.log('creating new draft');
    res.status(200).render('editDraft.hbs');
}
});

I am using Express and Mongoose. view engine is Handlebars.

Thanks for reading!

like image 651
omerkarj Avatar asked Nov 18 '13 21:11

omerkarj


People also ask

What does res redirect do in NodeJS?

The res. redirect() function redirects to the URL derived from the specified path, with specified status, a integer (positive) which corresponds to an HTTP status code. The default status is “302 Found”.

How do you Res send and redirect?

The res. redirect() function lets you redirect the user to a different URL by sending an HTTP response with status 302. The HTTP client (browser, Axios, etc.) will then "follow" the redirect and send an HTTP request to the new URL as shown below. const app = require('express')(); // The `res.

How do I redirect a login page in node JS?

The most obvious answer is to simply call res. redirect('/login.


1 Answers

I think the status 200 is throwing you off. Try using a 302 and it should work.

res.writeHead(302, {
    'Location': '/draft?id='+id
});
res.end();  
like image 162
AdvancedBeginner Avatar answered Sep 24 '22 12:09

AdvancedBeginner