How to return 404 using Iron Router

别等时光非礼了梦想. 提交于 2019-12-01 00:11:10

问题


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?


回答1:


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 );
  }
});



回答2:


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




回答3:


this.route('template404', {
  path: '/*'
}

Use it at the end of your Router.map, cause this catches every value - if you use at the begining every path will be caught to this

Of course you can make it more complex, for example:

this.route('template404', {
      path: '/posts/*'
    }


来源:https://stackoverflow.com/questions/27001298/how-to-return-404-using-iron-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!