user6885115
user6885115

Reputation:

Node Express return 404 custom message

I'm using NodeJS with Express and when an Endpoint is not found Express returns enter image description here

I'd like to return a Custom 404 Message instead of the above image whenever an Endpoint is miss-typed.

I've tried adding the following to app.js

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

But this just returns ALL endpoints with

Sorry can't find that!

Upvotes: 2

Views: 1939

Answers (1)

Alex
Alex

Reputation: 38519

You need to make that this the last 'route' in app.js

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

The order your specify your routes is important.

Upvotes: 3

Related Questions