Express.js 通过中间件验证路径参数的路由错误
Express.js error with route validated path param via middleware
我为以下路由获得了这个中间件:
router.param('userId', (req, res, next, val) => {
if (!/^\d+$/i.test(val)) {
globalResponse.sendResponse(res, 400, `la sintaxis de entrada no es válida para integer: «${val}»`, true);
} else {
next();
}
});
路线是:
router.get('/user/:userId', (req, res) => {
let userId = req.params.userId,
criteria = {};
criteria.userId = Number(userId);
users.findUserByCriteria(criteria)
.then(userFinded => {
if (userFinded != null) {
globalResponse.sendResponse(res, 200, userFinded, false);
} else {
globalResponse.sendResponse(res, 404, `Usuario ${userId} no Encontrado.`, true);
}
})
.catch(err => {
globalResponse.sendResponse(res, 500, err, true)
});
});
一切正常,错误是当尝试使用其他路由时 '/user/'
中间件没有此路径参数有效 'userId'
router.get('/user/all', (req, res) => {
users.findAllUsers()
.then(allUsers => {
globalResponse.sendResponse(res, 200, allUsers, false);
})
.catch(err => {
globalResponse.sendResponse(res, 500, err, true);
});
});
然后在 'user/all'
有效,这不是必需的
您可以进行一些更改以接受所有参数
router.param('userId', (req, res, next, val) => {
if(val=="all")
{
next();
}else if (!/^\d+$/i.test(val)) {
globalResponse.sendResponse(res, 400, "la sintaxis ...", true);
} else {
next();
}
});
并在您以后的代码中
let userId = req.params.userId,
if(userId=="all"){
users.findAllUsers()
.then(allUsers => {
globalResponse.sendResponse(res, 200, allUsers, false);
})
.catch(err => {
globalResponse.sendResponse(res, 500, err, true);
});
}
.........
......
你不需要这个路由器router.get('/user/all', (req, res) => {
我为以下路由获得了这个中间件:
router.param('userId', (req, res, next, val) => {
if (!/^\d+$/i.test(val)) {
globalResponse.sendResponse(res, 400, `la sintaxis de entrada no es válida para integer: «${val}»`, true);
} else {
next();
}
});
路线是:
router.get('/user/:userId', (req, res) => {
let userId = req.params.userId,
criteria = {};
criteria.userId = Number(userId);
users.findUserByCriteria(criteria)
.then(userFinded => {
if (userFinded != null) {
globalResponse.sendResponse(res, 200, userFinded, false);
} else {
globalResponse.sendResponse(res, 404, `Usuario ${userId} no Encontrado.`, true);
}
})
.catch(err => {
globalResponse.sendResponse(res, 500, err, true)
});
});
一切正常,错误是当尝试使用其他路由时 '/user/'
中间件没有此路径参数有效 'userId'
router.get('/user/all', (req, res) => {
users.findAllUsers()
.then(allUsers => {
globalResponse.sendResponse(res, 200, allUsers, false);
})
.catch(err => {
globalResponse.sendResponse(res, 500, err, true);
});
});
然后在 'user/all'
有效,这不是必需的
您可以进行一些更改以接受所有参数
router.param('userId', (req, res, next, val) => {
if(val=="all")
{
next();
}else if (!/^\d+$/i.test(val)) {
globalResponse.sendResponse(res, 400, "la sintaxis ...", true);
} else {
next();
}
});
并在您以后的代码中
let userId = req.params.userId,
if(userId=="all"){
users.findAllUsers()
.then(allUsers => {
globalResponse.sendResponse(res, 200, allUsers, false);
})
.catch(err => {
globalResponse.sendResponse(res, 500, err, true);
});
}
.........
......
你不需要这个路由器router.get('/user/all', (req, res) => {