javascript - Express.js 4 routes not matching with router.route - Stack Overflow

I'm trying to get the hang of router.route in Express 4. The docs make it sound awesome, but it�

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 mand line tool to make a standard app and then add routes/contacts.js that looks like this:

var express = require('express');
var router = express.Router();

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

module.exports = router;

Then in app.js add:

var contacts = require('./routes/contacts');

...

app.use('/contacts', contacts);

I'd expect http://localhost:8000/contacts/1 to match the route from contacts.js. However, I get an error that essentially indicates it's not matching any routes in contacts.js

Error: Not Found
    at Layer.app.use.res.render.message [as handle] (project1/app.js:31:15)
    at trim_prefix (project1/node_modules/express/lib/router/index.js:226:17)
    at c (project1/node_modules/express/lib/router/index.js:198:9)
    at Function.proto.process_params (project1/node_modules/express/lib/router/index.js:251:12)
    at next (project1/node_modules/express/lib/router/index.js:189:19)
    at next (project1/node_modules/express/lib/router/index.js:150:14)
    at next (project1/node_modules/express/lib/router/index.js:166:38)
    at Function.proto.handle (project1/node_modules/express/lib/router/index.js:234:5)
    at Layer.router (project1/node_modules/express/lib/router/index.js:23:12)
    at trim_prefix (project1/node_modules/express/lib/router/index.js:226:17)

If I add routes using a static prefix, it works as expected:

router.get('/1', function(req, res) {
  res.send('It worked!');
});

// http://localhost:8000/contacts/1 says "It worked!"

Any tips on what I'm doing wrong?

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 mand line tool to make a standard app and then add routes/contacts.js that looks like this:

var express = require('express');
var router = express.Router();

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

module.exports = router;

Then in app.js add:

var contacts = require('./routes/contacts');

...

app.use('/contacts', contacts);

I'd expect http://localhost:8000/contacts/1 to match the route from contacts.js. However, I get an error that essentially indicates it's not matching any routes in contacts.js

Error: Not Found
    at Layer.app.use.res.render.message [as handle] (project1/app.js:31:15)
    at trim_prefix (project1/node_modules/express/lib/router/index.js:226:17)
    at c (project1/node_modules/express/lib/router/index.js:198:9)
    at Function.proto.process_params (project1/node_modules/express/lib/router/index.js:251:12)
    at next (project1/node_modules/express/lib/router/index.js:189:19)
    at next (project1/node_modules/express/lib/router/index.js:150:14)
    at next (project1/node_modules/express/lib/router/index.js:166:38)
    at Function.proto.handle (project1/node_modules/express/lib/router/index.js:234:5)
    at Layer.router (project1/node_modules/express/lib/router/index.js:23:12)
    at trim_prefix (project1/node_modules/express/lib/router/index.js:226:17)

If I add routes using a static prefix, it works as expected:

router.get('/1', function(req, res) {
  res.send('It worked!');
});

// http://localhost:8000/contacts/1 says "It worked!"

Any tips on what I'm doing wrong?

Share Improve this question edited Jun 8, 2014 at 23:45 newz2000 asked Jun 8, 2014 at 21:52 newz2000newz2000 2,6401 gold badge24 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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);
  })

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745289873a4620791.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信