0
我有一个nodejs服务器应用程序,它向客户端应用程序提供一个API。 此前,应用程序会发送一个AJAX请求,其中包含请求对象中的一个action参数req.body.action,并将其传递给主路由(我的意思是“/”),以便根据此参数执行操作。 但是,我需要将任何AJAX POST请求的路由从主路由'/'更改/重定向到动作特定路由'/ {action route}'。如何将POST请求重定向到Nodejs中的其他路由
N.B .:我想为每个未更新客户端应用程序的用户提供向后兼容性,以考虑此更改。即不能修改这些用户的AJAX请求代码。
我已经尝试了下面的这段代码,但它不起作用。
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 the request for action-1 here but it's not routed
});
app.post("/action-2", function (req, res) {
//would have proceeded the request for action02 here but it's not routed
});