0

I am facing a very strange issue. I have two routes configured. one for my dashboard and one for external API.

  dashboard = require('./routes/dashboard')(passport);
  api = require('./routes/api');

  app.use('/', dashboard);
  app.use('/api', api);

The following two routes are defined in my api.js routes file.

router.post('/somepostlink',function(){
   // this is reachable from request query.
})

router.get('/somegetlink',function(){
   // this is NOT reachable from request query.
})

I have this defined in my dashboard.js routes files :

// If no route is matched, control is transferred to the block of code below

router.get('*',function (request,response) {
console.log("Route not found");


return response.send("OOPs :( \nSeems like the page you are looking 
for, isn't available with us.").status(404);

Now, POST request to localhost:3000/api/somepostlink works. But, GET request to localhost:3000/api/somegetlink shows

"OOPs :( \nSeems like the page you are looking for, isn't available with us."

Nikhil
  • 467
  • 10
  • 22
  • Put the catch all `*` route in the file where `app` is and do `app.get('*', ...)` after you setup your API and dashboard routers. – Andrew Li Sep 02 '17 at 05:34
  • @AndrewLi I have tried that but it still tries to find it in dashboard.js routes file and shows dashboard's home page ... the one served with ( '/' ) route – Nikhil Sep 02 '17 at 05:39

1 Answers1

0

In routers, order is important. Change your routers in places. And change catch 404 error handler.

app.use('/api', api);
app.use('/', dashboard);

app.use(function(req, res, next) {
 var err = new Error('Not Found');
 err.status = 404;
 next(err);
});
temakozyrev
  • 372
  • 1
  • 7