繁体   English   中英

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

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

我想将404(以及500)处理等添加到我的快速应用文件中。 当他们遇到404错误时,我可以向客户端显示简单文本,但是现在我想显示html而不是文本。 您可能会看到,我的404.html位于/ app中。

到目前为止,我的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.
});

这是我进入控制台的错误:

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

我无法理解此错误(显然)。 我还使用Express 4。

如果您只想显示静态html,则另一种选择是使用重定向。 在您的情况下,该路径将相对于/ app。

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

根据快速文档( 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