Skip to content Skip to sidebar Skip to footer

Express.js 4 Routes Not Matching With Router.route

I'm trying to get the hang of router.route in Express 4. The docs make it sound awesome, but it's just not working for me. If I use the command line tool to make a standard app and

Solution 1:

Router paths are relative to the mounted path. So your contacts router would instead just be:

router.route('/:contactid')
  .get(function(req, res) {
    res.send('(get) It worked ' + req.params.contactid);
  })

Solution 2:

I think this should work (does for me)

In routes/contacts.js

/* Created by matthias on 6/9/14. */var express = require('express');
var router = express.Router();

router.get('/:contactid', function(req, res) {
        res.send('(get) It worked ' + req.params.contactid);
    });

module.exports = router;

Then in app.js

var contacts = require('./routes/contacts');
var app = express();
app.use('/contacts', contacts);

Works for me: localhost:3000/contacts/:3

Predictably getting: (get) It worked 3

Post a Comment for "Express.js 4 Routes Not Matching With Router.route"