Node.js - Express.js URL parameters validation
<p>I'm a node.js/express.js newbie. How can I validate <code>:id</code> parameters? I would like to pass only numbers into <code>:id</code> parameters. If <code>:id</code> is a string or contain once of them, I would to display a 404 error like a zend framework routing <a href="http://framework.zend.com/manual/current/en/user-guide/routing-and-controllers.html" rel="nofollow">http://framework.zend.com/manual/current/en/user-guide/routing-and-controllers.html</a></p>

<p><strong>/routes/users.js</strong> </p>

<pre><code>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;
</code></pre>

<p>I tried to change </p>

<p><code>router.get('/:id?/', function(req, res, next)</code> </p>

<p>to </p>

<p><code>router.get('/[0-9]+?/', function(req, res, next)</code></p>

<p>but </p>

<pre><code>localhost:3000/users/ab/
</code></pre>

<p>works and display <code>single-users</code> page and I want it..</p>

<p><strong>SOLUTION SUGGESTED BY LUCAS COSTA</strong></p>

<pre><code>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;
</code></pre>

<p>or </p>

<pre><code>router.get('/:id([0-9]+)?/', function(req, res)
</code></pre>