【问题标题】:Why is it that when I send a simple file from express, the client consumes a lot of memory?为什么当我从express发送一个简单的文件时,客户端会消耗大量内存?
【发布时间】:2021-11-03 02:59:07
【问题描述】:

当我从浏览器打开 html 文件时,它会使用以下内容:

但是当我从 Express 服务器发送相同的文件时,内存消耗明显更高:

确实,这不是一个巨大的内存消耗,但它是一个非常明显的区别,为什么会发生这种情况?,是 Express 向客户端发送我不知道的东西(标题,cookie,什么?)?

在服务器上,我只有一个 JavaScript 文件,其中包含一个使用 Express 的 sendFile 函数发送 html 文件的路由:

const express = require('express');
const { join } = require('path');

const server = express();

server.get('/', (_, res) => {
    res.sendFile(join(__dirname, 'render.html'));
});

server.listen(3000, () => {
    console.log('Server is running in port 3000');
});

而您发送给客户端的 HTML 文件仅包含以下内容:

<!DOCTYPE html>
<html lang="es">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <header>
            <h1>This is Header</h1>
        </header>
        <main>
            <section>
                <h1>Section 1</h1>
            </section>
            <section>
                <h1>Section 2</h1>
            </section>
        </main>
        <footer>
            <h2>This is Footer</h2>
        </footer>
    </body>
</html>

没有别的了。

我在 Google Chrome 和 Edge 上试过,希望你能帮助我理解 c:

【问题讨论】:

    标签: node.js express server client sendfile


    【解决方案1】:

    它们既不是 HTTP 标头,也不是 cookie……更不用说服务器了,问题在于浏览器的扩展!!!

    显然,当我在本地打开 HTML 文件(使用“文件:”协议)时,扩展没有运行。

    这对我来说很有趣,我不知道浏览器扩展如何工作以及它们的生命周期,但显然扩展共享当前选项卡中的进程和内存,即它们共享进程和内存当前正在运行的应用程序,它不是我的内存消耗应用程序,而是浏览器扩展进程。

    【讨论】:

      猜你喜欢
      • 2019-08-05
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多