0

I need your help concerning routes in node.js. All newly created routes work fine locally, but not on the remote server after pushing changes. Other urls are still working.

Below is the middleware to capture 404 errors:

// All new routes fall here
app.all('*', function (req, res) {
    res.status(404).json({
        error: 'Not Found'
    });
});

I can confirm that I verified all the config files and I don't know why this is happening.

Could someone explain this to me? Maybe I missed something between local and remote?

Andrew Myers
  • 2,754
  • 5
  • 32
  • 40

4 Answers4

0

I found some similar answers here, here and in the oficial Express page here

Basically, the best way to redirect a 404 error is doing something like this:

app.use(function (req, res, next) {
  res.status(404).send("Sorry can't find that!")
})

Read more about it in the links at the top.

Hope it help you.

Fernando Paz
  • 532
  • 6
  • 19
  • Thank you @Fernando but it doesn't solve my problem, my question is : why others routes works locally and remotely and others (new) works only locally? –  May 07 '18 at 13:08
  • 1
    What errors you getting in case of "others (new) not working in remote " .You can see the server console. right? – Mohammad Raheem May 08 '18 at 05:23
0
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
})

You can use the above code to capture 404 errors.

0

It's possible if you made changes in server and didn't restart nodejs. If you use PM2 try pm2 restart app_name

You can use pm2 start app.js --watch to let pm2 actively watches for any changes in file and restart for you.

For more info about pm2 - http://pm2.keymetrics.io/docs/usage/quick-start/#usage

Deepak M
  • 849
  • 8
  • 17
0

use following code. it will work .

app.route('/*').get(function(req, res) {
      res.status(404).send("page not found!")
    });
Venkatesh
  • 160
  • 1
  • 2
  • 11