Reputation: 3501
I know this question has been asked here before but I could not find any solution going through them so I have to ask it myself.
Here is my app.js code
var express = require('express');
var exphbs = require('express-handlebars');
var router = express.Router();
var paymentCampaigns = require('./routes/paymentCampaigns');
var app = express();
app.use('/payment-campaigns', paymentCampaigns);
//create server and listen to the port
http.createServer(app).listen(app.get('port'), function(){
winston.log('info', 'The server has started');
});
app.use(function(req, res, next){
var err = new Error('Not Found');
err.status = 404;
next(err);
});
module.exports = app;
Here is the code for paymentCampaigns.js
var express = require('express');
var router = express.Router();
router.get('/add', function(req, res){
res.render('add_campaign');
});
router.get('/pending', function(req, res){
res.send('hello this is pending');
});
module.exports = router;
Whenver I type localhost:8000/payment-campaigns/pending it takes me to the 404 error page. The /add route takes me to the add_campaign page. What am I doing wrong? I have other routes there as well and all of them work fine except this one.
Upvotes: 3
Views: 2672
Reputation: 3501
The issue was with the way routes were defined in my code. The pending route was defined at the end and there was another get route with param before it.
So something like this:
var express = require('express');
var router = express.Router();
router.get('/add', function(req, res){
res.render('add_campaign');
});
router.get('/:campaignId', function(req,res) {
//get the campaign detail using the campaignId
});
router.get('/pending', function(req, res){
res.send('hello this is pending');
});
module.exports = router;
I had to move all my only "get" routes before the /:campaignId route and it started working.
Upvotes: 1