【问题标题】:Is there any advantage in storing static html files in a server variable on a node-express server?将静态 html 文件存储在 node-express 服务器上的服务器变量中是否有任何优势?
【发布时间】:2017-05-05 02:11:15
【问题描述】:

我正在使用 express 运行一个小型节点服务器,并且在某些时候我决定将我的静态 html 内容存储在我的服务器上的变量中,而不是从磁盘提供它们,认为服务器应该将它们存储在 RAM 中方式,因此为他们服务的速度会稍微快一些。

但是,在寻找有关该主题的可靠信息时,我感到很失望,并且无法找到有关该主题的任何信息。因此,我以这种方式询问有关该主题的想法(甚至是有关该主题的良好阅读材料)。

最好, 福赫拉克

示例代码:

const   express = require('express')
    ,   app = express()
    ,   server = require('http').createServer(app)
    ,   server = require('http').createServer(app)
    ,   server_port = 8080
    ,   server_ip_address = 'localhost';

server.listen(server_port, server_ip_address, () => {
    console.log('listening on port '+ server_port);
});

app.use('/index', express.static('/Public/index.html'));

/*** vs ***/

const index = fs.readFileSync('/Public/index.html').toString();

app.get('/index', (req, res, next) => {
    res.status(200).send(index);
});

【问题讨论】:

    标签: node.js express static-content


    【解决方案1】:

    如果文件很小,为什么不呢?

    最好将文件缓存在变量中以使用内存 io 比 hdd io 更便宜。

    但请记住跟踪文件更改并重新阅读:

    const staticFiles = {
      'index': fs.readFileSync(__dirname+'/Public/index.html').toString();
    };
    fs.watch(__dirname+'/Public/index.html', () => {
      staticFiles.index = fs.readFileSync(__dirname+'/Public/index.html').toString();
    });
    
    app.get('/index', (req, res, next) => {
        res.status(200).send(staticFiles.index);
    });
    

    但我认为 express-static 中间件已经做到了。

    阅读这个:http://expressjs.com/en/starter/static-files.html 和这个:https://github.com/expressjs/serve-static/blob/master/index.js

    但我没有看到它正在缓存文件到变量。



    我不关心这个问题,因为我在 nginx 后面使用 nodejs 应用程序,所以所有缓冲和静态文件服务都是由 nginx 完成的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      相关资源
      最近更新 更多