简体   繁体   English

Express 4 404错误处理找不到模块'html'错误

[英]Express 4 404 error handling cannot find module 'html' error

I want to add 404 (and also 500) handling etc to my express app file. 我想将404(以及500)处理等添加到我的快速应用文件中。 Im able to display simple text to the client when they get a 404 error, but now I want to display html instead of text. 当他们遇到404错误时,我可以向客户端显示简单文本,但是现在我想显示html而不是文本。 My 404.html is located in /app as you probably can see. 您可能会看到,我的404.html位于/ app中。

So far I have this in my app.js: 到目前为止,我的app.js中包含以下内容:

.....
app.use(express.static(__dirname + '/app'));
app.get("/app", function(req, res) {
  res.redirect("index");
});
.....
.....
.....
app.use(function(req, res, next) {
  res.status(400);
  res.render('404.html', {title: '404: File Not Found'} );
  //Also tried taking away the .html after 404 it still doesn't work.
});

And this is the error Im getting in console: 这是我进入控制台的错误:

Error: Cannot find module 'html' at Function.Module._resolveFilename (module.js:338:15) 
at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require 
(module.js:380:17) at new View 
(H:\Developer\Github\angularApp\node_modules\express\lib\view.js:43:49) at 
Function.app.render 
(H:\Developer\Github\angularApp\node_modules\express\lib\application.js:503:12) at 
ServerResponse.res.render 
(H:\Developer\Github\angularApp\node_modules\express\lib\response.js:818:7) at Layer.port 
[as handle] (H:\Developer\Github\angularApp\app.js:33:7) at trim_prefix 
(H:\Developer\Github\angularApp\node_modules\express\lib\router\index.js:255:15) at 
H:\Developer\Github\angularApp\node_modules\express\lib\router\index.js:216:9

Im not able to understand this error (obviously). 我无法理解此错误(显然)。 Im also using Express 4. 我还使用Express 4。

Another option is to use redirects if you just want to show the static html. 如果您只想显示静态html,则另一种选择是使用重定向。 The path will be relative to /app in your case. 在您的情况下,该路径将相对于/ app。

// Error handling - if all other routes fail
app.use(function(req, res, next){
    res.status(404);
    res.redirect('/404.html')
});

According to the express documentation ( http://expressjs.com/faq.html ) 500 errors are handled like normal middleware ie 根据快速文档( http://expressjs.com/faq.html ),像普通中间件一样处理500个错误,即

app.use(function(err, req, res, next){
  console.error(err.stack);
  res.send(500, 'Something broke!');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM