每当我尝试使用第一条路的时候,我得到的第二个代替。
有什么办法可以覆盖第二路由时,我收到了'/ allAnswered的要求?
router.get('/allAnswered', verify.verifyAdmin, async (req, res) => {
await reportService.getAllAnswered(req.query.userId, req.query.sessionId, (response) => {
res.status(response.status).send(response);
})
});
router.get('/:reportId', verify.verifyAdmin, async (req, res) => {
await reportService.getReportById(req.params.reportId, (response) => {
res.status(response.status).send(response);
})
});
是。你应该写这样的事情:
router.get('/reports/:reportId,verify.verifyAdmin, async (req, res) => {
await reportService.getReportById(req.params.reportId, (response) => {
res.status(response.status).send(response);
})
});
不要以为表达允许这样做,你可以考虑使用这种风格:
router.get('/:reportId', verify.verifyAdmin, async (req, res) => {
if (req.params.reportId === 'allAnswered') {
await reportService.getAllAnswered(req.query.userId, req.query.sessionId,
(response) => {
res.status(response.status).send(response);
})
} else {
await reportService.getReportById(req.params.reportId, (response) => {
res.status(response.status).send(response);
})
}
});