admin管理员组文章数量:1356825
I'm a node.js/express.js newbie. How can I validate :id
parameters? I would like to pass only numbers into :id
parameters. If :id
is a string or contain once of them, I would to display a 404 error like a zend framework routing .html
/routes/users.js
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/:id?/', function(req, res, next) {
var id = req.params.id;
if(id){
res.render('single-users', { title: 'Single ' + id });
}else {
res.render('users', { title: 'All users' });
}
});
module.exports = router;
I tried to change
router.get('/:id?/', function(req, res, next)
to
router.get('/[0-9]+?/', function(req, res, next)
but
localhost:3000/users/ab/
works and display single-users
page and I want it..
SOLUTION SUGGESTED BY LUCAS COSTA
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/:id(\\d+)?/', function(req, res) {
var id = req.params.id;
if(id){
res.render('single-users', { title: 'Single ' + id });
}else {
res.render('users', { title: 'All users' });
}
});
module.exports = router;
or
router.get('/:id([0-9]+)?/', function(req, res)
I'm a node.js/express.js newbie. How can I validate :id
parameters? I would like to pass only numbers into :id
parameters. If :id
is a string or contain once of them, I would to display a 404 error like a zend framework routing http://framework.zend./manual/current/en/user-guide/routing-and-controllers.html
/routes/users.js
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/:id?/', function(req, res, next) {
var id = req.params.id;
if(id){
res.render('single-users', { title: 'Single ' + id });
}else {
res.render('users', { title: 'All users' });
}
});
module.exports = router;
I tried to change
router.get('/:id?/', function(req, res, next)
to
router.get('/[0-9]+?/', function(req, res, next)
but
localhost:3000/users/ab/
works and display single-users
page and I want it..
SOLUTION SUGGESTED BY LUCAS COSTA
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/:id(\\d+)?/', function(req, res) {
var id = req.params.id;
if(id){
res.render('single-users', { title: 'Single ' + id });
}else {
res.render('users', { title: 'All users' });
}
});
module.exports = router;
or
router.get('/:id([0-9]+)?/', function(req, res)
Share
Improve this question
edited Mar 18, 2016 at 16:25
Alessandro Corradini
asked Mar 16, 2016 at 17:53
Alessandro CorradiniAlessandro Corradini
4991 gold badge9 silver badges29 bronze badges
2 Answers
Reset to default 4You can provide the regex:
router.get('/:id(\\d+)/', function (req, res, next){
// body
});
Docs
Why not just do the integer-only check inside of the main code block and then return the 404 conditionally?
router.get('/:id', function(req, res, next) {
var id = req.params.id;
if(id && string.match(/^[0-9]+$/) != null)}
res.render('single-users', { title: 'Single ' + id });
}else if(string.match(/^[0-9]+$/) == null){
res.status(404).render('your-404-view');
}else{
res.render('users', { title: 'All users' });
}
});
本文标签: javascriptNodejsExpressjs URL parameters validationStack Overflow
版权声明:本文标题:javascript - Node.js - Express.js URL parameters validation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744071466a2585965.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论