【发布时间】:2012-02-05 11:27:51
【问题描述】:
我正在尝试使用更好的 url 端点提供一些静态文件。
例如,/home 将服务于/public/home.html。
我可能可以在路由配置中使用res.sendfile(),但是在生产模式下 sendfile 不会缓存文件输出,所以我不太确定这是否是一个好的解决方案。
如何设置路由以充当 html 文件的别名?
【问题讨论】:
我正在尝试使用更好的 url 端点提供一些静态文件。
例如,/home 将服务于/public/home.html。
我可能可以在路由配置中使用res.sendfile(),但是在生产模式下 sendfile 不会缓存文件输出,所以我不太确定这是否是一个好的解决方案。
如何设置路由以充当 html 文件的别名?
【问题讨论】:
试试这个。
var express = require('express');
var app =express.createServer();
// ------
app.get('/home', function(req,res){
res.sendfile(__dirname + '/public/home.html');
});
【讨论】:
有一个名为node-static 的模块提供缓存功能。您也可以只使用符号链接。使用 nginx 的 try_files 或其他非 node.js 反向代理可能会更好地为您提供此功能。
有一段时间,express 中内置了一个 staticCache 中间件来进行缓存。是removed from connect in January 2014.Here's the github issue where TJ explains staticCache being deprecated。
【讨论】: