0
クライアント側アプリケーションにAPIを提供するnodejsサーバーアプリケーションがあります。 以前は、リクエストオブジェクトreq.body.actionのアクションパラメータを含むAJAXリクエストをメインルート(私は '/')に送信し、このパラメータのアクションベースを進めます。 しかし、AJAX POST要求のルートをメインルート「/」からアクション固有のルート「/ {アクションルート}」に変更/リダイレクトする必要があります。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
});