Expressjs / Node.js – res.redirect() not loading page

Your request is POST ($.post) and you route is app.del, so it never gets to res.redirect inside app.del route.

Why don’t you use app.post?

Updated:

Assuming $.post sends HTTP DEL request here what is happening: server sends 302 response with no data but browser never sends another request to GET route as server instructs it (or does jQuery handle redirects too? Not sure). res.redirect() is actual HTTP response not some internal server-side instruction to re-route the request to another route like you can do in ASP.NET (and which is wrong actually)… Route is designed to receive request, reply with the response and forget about it. You need to separate routes from actual functions processing them, then you will be able to call that function instead of sending redirect.

Code suggestions

In app.del(‘/team/:key’ …

...
    retStatus = 'Success';
    // res.redirect('/team');
    res.send({
      retStatus : retStatus,
      redirectTo: '/team',
      msg : 'Just go there please' // this should help
    });
...

Client-side in $.post(‘/team/’ …

...
    $.post('/team/' + teamId, { _method : 'delete' }, function(response) {
        console.log(response);
        if(response.retStatus === 'Success') {
            // not sure what did you mean by ('/team' && '/team' !== "")
            // if('/team' && '/team' !== "") {
            if (response.redirectTo && response.msg == 'Just go there please') {
                window.location = response.redirectTo;
            }
        }
    });
...

Not sure it will work though because I don’t understand what your getAllTeams does and why you store teamList in req. If you want to store in session, than assuming the middleware is correctly configured you need to use req.session. If you need to store it only within request and your getAllTeams prepares this list of teams it is better to store in res.locals (like res.locals.teamList). And make sure your getAllTeams calls next. So basically your getAllTeams should look like this:

function getAllTeams (req, res, next) {
    res.locals.teamList = [/* whatever */];
    next();
}

And then you can use res.locals.teamList in your route handler instead of req.teamList.

res.render('team', {teamsList : res.locals.teamsList} );

And ‘team’ template also can have a problem…

Express advice 🙂

Also the way you use Express makes it very difficult to extend/manage application. I don’t remember where exactly, but somewhere in docs they write that Express is supposed to be used as the foundation for your application framework, not as a complete framework like most PHP frameworks are. It gives you a lot of power and flexibility, but also makes it necessary to think about your application architecture well in advance.

The most powerful feature of express is that you can have any route handled by many route-specific handlers/middlewares passing control to each other via next(). I have a static table that defines which handlers are used on each route allowing to see the whole application with 30 or so routes on one page. And this table is used to assemble the routing dynamically when the application starts. It leads to a lot of flexibility, manageability (I can move/copy-paste handlers from route to route – each handler is represented as a single word) and code re-use. I also use the same hash of routes definition in the client for client-side routing.

Leave a Comment