当前位置: 首页 > 面试题库 >

如何将404错误重定向到ExpressJS中的页面?

况安然
2023-03-14
问题内容

我不知道执行此操作的功能,有人知道吗?


问题答案:

我发现这个例子很有帮助:

https://github.com/visionmedia/express/blob/master/examples/error-
pages/index.js

所以实际上是这部分:

// "app.router" positions our routes
// above the middleware defined below,
// this means that Express will attempt
// to match & call routes _before_ continuing
// on, at which point we assume it's a 404 because
// no route has handled the request.

app.use(app.router);

// Since this is the last non-error-handling
// middleware use()d, we assume 404, as nothing else
// responded.

// $ curl http://localhost:3000/notfound
// $ curl http://localhost:3000/notfound -H "Accept: application/json"
// $ curl http://localhost:3000/notfound -H "Accept: text/plain"

app.use(function(req, res, next){
  res.status(404);

  // respond with html page
  if (req.accepts('html')) {
    res.render('404', { url: req.url });
    return;
  }

  // respond with json
  if (req.accepts('json')) {
    res.send({ error: 'Not found' });
    return;
  }

  // default to plain-text. send()
  res.type('txt').send('Not found');
});


 类似资料:
  • 问题内容: 我正在编写servlet,以防万一,我将重定向到自定义错误页面,因为这样做是这样的。 在web.xml中 在Servlet中, 但是这里没有显示,我要去哪里出错了,谁能解释一下我? 问题答案: 问题是您捕获了Exception,因此没有Exception将离开您的方法。如果匹配项(相同或相同的子类)离开您的方法,则只会重定向错误页面。 您应该将捆绑包重新扔入例如: 不幸的是,如果我们谈

  • 我将spring boot security用作restful服务的ACL。安全适配器如下所示 userdetailservice的快照 我不确定是哪个部分导致了重定向。

  • 在TYPO3 v10中,您不能使用$TSFE-

  • 问题内容: 我正在nodejs / expressjs中构建购物车应用程序。在用户登录后重定向到正确的页面时,我遇到了一些问题。我发现有几篇文章密切相关,但是我从给定的文章中进行修复的所有尝试都没有成功。 发生的情况是,用户输入其登录信息,然后单击登录按钮。Ajax请求将数据发送到此访问功能。该功能首先检查用户是否存在于数据库中。如果是,那么它将继续比较bcrypt密码,如果它们相等,则它们成功登

  • 我为重定向创建了一个简单的@Controller: 描述符是: MVC-配置在spring-mvc.xml是: webapp结构见附件: 当我运行tomcat时,它会在url上重定向我:http://localhost:8080/test/access_denied. 但是有404错误。

  • 问题内容: 我试图将自定义中间件注入OWIN管道,该中间件包装来自MS 的可用中间件,以便在AngularJS中支持HTML 5模式。我一直在遵循此指南:http : //geekswithblogs.net/shaunxu/archive/2014/06/10/host-angularjs-html5mode-in- asp.net-vnext.aspx 从我可以收集到的信息来看,中间件将请求传