Reputation: 1014
I've applied the routes to my application like this:
var express = require('express');
var app = express();
var router = express.Router();
//localhost:8080/api/story
router.get('/story', function(req, res){
res.send('welcome to our story');
})
//localhost:8080/api
app.use('/api', router);
//localhost:8080/user/02213
router.get('/user/:id', function(req , res){
console.log(req.params.id);
});
localhost:8080/user/02213
not working at the moment. Do I need to create a new router instead or?
Upvotes: 8
Views: 22314
Reputation: 5359
The problem is that id
not recognized in the request,
You should call it like that : req.params.id
Upvotes: 0
Reputation: 203231
Yes, you need to create a new router, because router
will only be used for requests that start with /api
:
//localhost:8080/api/story
router.get('/story', function(req, res){
res.send('welcome to our story');
})
//localhost:8080/api
app.use('/api', router);
//localhost:8080/user/02213
var anotherRouter = express.Router();
anotherRouter.get('/user/:id', function(req , res){
console.log(req.params.id);
res.end();
});
app.use('/', anotherRouter);
Upvotes: 14