【问题标题】:How to set Express route alias to html file?如何将 Express 路由别名设置为 html 文件?
【发布时间】:2012-02-05 11:27:51
【问题描述】:

我正在尝试使用更好的 url 端点提供一些静态文件。

例如,/home 将服务于/public/home.html

我可能可以在路由配置中使用res.sendfile(),但是在生产模式下 sendfile 不会缓存文件输出,所以我不太确定这是否是一个好的解决方案。

如何设置路由以充当 html 文件的别名?

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    试试这个。

    var express = require('express');
    var app =express.createServer();
    // ------
    app.get('/home', function(req,res){
     res.sendfile(__dirname + '/public/home.html');
    }); 
    

    【讨论】:

    • 谢谢,这是我目前正在使用的,但建议不要这样做,因为它不会缓存文件并且对生产模式不利。
    • 如果它不能真正解决您与缓存相关的问题,这怎么能成为公认的答案?
    【解决方案2】:

    有一个名为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

    【讨论】:

    猜你喜欢
    • 2015-03-28
    • 2023-04-04
    • 2017-07-09
    • 2020-10-05
    • 1970-01-01
    • 2020-06-17
    • 2020-06-16
    • 2017-11-23
    • 2021-02-07
    相关资源
    最近更新 更多