Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return 404 using Iron Router

When I hit a route that doesn't exist on my Meteor app that uses IR, I get a 200 response with an HTML that (when rendered on a browser) displays a js error on console saying that No route found for path: "/aRoute".

How can a make it return 404?

like image 902
Tomas Romero Avatar asked Nov 18 '14 18:11

Tomas Romero


2 Answers

There don't seem to be a correct (or even working?) way of handling real 404's right now. See this issue for example: https://github.com/EventedMind/iron-router/issues/1055

Even when you try ways which should work, you'll still end up with a 200 status code. Like this code below which should work:

this.route( 'pageNotFound', {
  path: '/(.*)',
  where: 'server',
  action: function() {
    this.response.writeHead(404);
    this.response.end( html );
  }
});
like image 67
Kristoffer K Avatar answered Sep 18 '22 20:09

Kristoffer K


I find this much easier way to show page not found. In router.js

Router.configure({
    layoutTemplate: "layout",
    loadingTemplate: "loading",
    notFoundTemplate: "notFound"
})

Here "notFound" could be any template where you want to show 404 error

like image 32
Kais Avatar answered Sep 21 '22 20:09

Kais