mo30
mo30

Reputation: 89

too many redirects in express

I want to handle all my cookies and session stuff in a function then redirect to the destination path. I use this function in Express:

app.all('*', function(req, res, next){
    if('_id' in req.session)
        next()
    else if('userInfo' in req.cookies){
        req.session._id = req.cookies.userInfo._id
        next()
    } else {
        res.redirect('../login')
    }
    res.end()
})

but browser print this error: net::ERR_TOO_MANY_REDIRECTS
what's the problem?

Upvotes: 1

Views: 3637

Answers (1)

user8377060
user8377060

Reputation:

This error occurs when a web browser is redirecting you to another web page which then that web page redirects you back to the same browser again. Your configurations are wrong. app.all('*', ..) runs again and again whenever a request is made, which causing repetition.

You need to have better configurations than app.all(*..) //all. You need to specify it more so that it doesn't repeat itself.

I would say limit it down to some urls and not all of them. You could do

app.get('/cookieCheckerPage', function(req, res){
//code here
});

Also please see Error 310 (net::ERR_TOO_MANY_REDIRECTS):

Upvotes: 1

Related Questions