如何在node.js中请求另一个路由? 我有POST请求,成功执行POST请求后,我想执行GET请求以获取该用户的日期。
app.post("/childrens", checkUser, async (req, res, next) => {
...
const childrenResponse = await Childrens.create({
...children,
id_kindergarten,
type: 2
})
//here I want make get request after successful POST request
const data = app.get("/childrens/2");
res.send(data);
}
答案 0 :(得分:3)
尝试像这样使用handle
方法:
req.method = "GET";
req.url = "/childrens/2";
app._router.handle(req, res, next);
答案 1 :(得分:2)
通常,此操作由包含其他资源的303 See Other
标头的Location
处理。就您而言:
res
.set('Location', '/childrens/2')
.status(303)
.send();
答案 2 :(得分:0)
像这样尝试,就我而言就可以了。
app.use('/users', function(req, res, next){
console.log('users');
});
app.use('/test', function(req, res, next){
console.log('test');
req.url = '/users';
return app._router.handle(req, res, next);
});
输出:
GET /test - - ms - -
test
users