res.redirect doesn't redirect Node/Express.js

僤鯓⒐⒋嵵緔 提交于 2019-12-20 07:04:37

问题


I'm using Express for my webapp and I'm checking to see if some value doesn't exists in session, redirect the user to instagram login page. But in the console I keep getting 302 code and the browser doesn't redirect the user here is my code. I'm sending my requests using Ajax.

    function requireLogin (req, res, next) {
     if (!req.token) 
        res.redirect('/auth');
     else 
        next();      
    };

 app.get('/auth',function(req,res,next){
   res.redirect("https://api.instagram.com/oauth/authorize/?client_id="+client_id+"&redirect_uri="+redirect_uri+"&score=public_content+likes+comments+follower_list+relationships&response_type=code");
});

I checked and app.get('/auth') gets called but it seems the second redirect to instagram doesn't do anything.


回答1:


The problem was that res.redirect does not redirect the user if you are using Ajax. So I passed the url to client side and used window.location.replace(url);




回答2:


Are you using Ajax ? If yes, you can't directly redirect, but send back the URL, and the front end will redirect to the URL your server sent.



来源:https://stackoverflow.com/questions/35182383/res-redirect-doesnt-redirect-node-express-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!