ajax - How to Redirect POST Request to another Route in Nodejs -
i have nodejs server application provides api client-side application. previously, app send ajax request contains action parameter in request object, req.body.action, main route (i mean '/') proceed action base on parameter. however, need change/redirect route of ajax post request main route, '/', action specific route, '/{action route}'.
n.b.: want allow backward compatibility every user hasn't updated client side app take change in consideration. i.e, can't modify ajax request code users.
i have tried code below not work.
app.use(bodyparser.json() ); app.post('/', function(req, res){ if( (req.body.action) && (req.body.action === 'action-1')){ res.redirect(307, '/action-1'); } if( (req.body.action) && (req.body.action === 'action-2')){ res.redirect(307, '/action-2'); } }); app.post("/action-1", function (req, res) { //would have proceeded request action-1 here it's not routed }); app.post("/action-2", function (req, res) { //would have proceeded request action02 here it's not routed });
you can try way:
app.use(bodyparser.json() ); app.post('/', function(req, res){ if( (req.body.action) && (req.body.action === 'action-1')){ return routes.act1(req, res); } if( (req.body.action) && (req.body.action === 'action-2')){ return routes.act2(req, res); } }); app.post("/action-1", routes.act1); app.post("/action-2", routes.act2);
it's not redirect, works.
Comments
Post a Comment