app.use('/static', express.static('public'));现在,你就爱可以通过带有 “/static” 前缀的地址来访问 public 目录下面的文件了。
http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html常见问题
如何处理 404 ?
在 Express 中,404 并不是一个错误(error)。因此,错误处理器中间件并不捕获 404。这是因为 404 只是意味着某些功能没有实现。也就是说,Express 执行了所有中间件、路由之后还是没有获取到任何输出。你所需要做的就是在其所有他中间件的后面添加一个处理 404 的中间件。如下:
app.use(function(req, res, next) {
res.status(404).send('Sorry cant find that!');
});Express 支持哪些模板引擎?
Express 支持任何符合 (path, locals, callback) 接口规范的模板引擎。
如何渲染纯 HTML 文件?
不需要!无需通过 res.render() 渲染 HTML。你可以通过 res.sendFile() 直接对外输出 HTML 文件。如果你需要对外提供的资源文件很多,可以使用 express.static() 中间件。









