Pylot
Pylot

Reputation: 261

Is there a way to handle the Cannot GET error in NodeJS?

I'm working on a search functionality for my application, but when users type something on the search bar that doesn't exist on the app it redirects them to the "Cannot GET /Search/whatevertheysearch" page. This is ok, but I'd like to handle it with more care. Is there a way to edit the html of this page, or handle the error?

Upvotes: 0

Views: 783

Answers (1)

Noodles
Noodles

Reputation: 4080

Since this is basically a 404, you could handle this by using this:

app.use((req, res, next) => {
  res.status(404).render('status');
});

And in stead of .render() you could also use send() or sendFile().

Upvotes: 1

Related Questions