从快递服务器发送 HTML 字符串与 HTML 文件之间的区别?

Difference between sending HTML string vs HTML file from express server?

我正在编写一个 Cloud Function 作为快速服务器,需要:

示例:

index.html

<html>
  <head>
    <title>Page Title</title>
  </head>
  <body>
    <p>Some text</p>
  </body>
</html>

问题

这是:

res.sendFile(path.join(__dirname + '/index.html'));

与此不同?

res.send('
  <html>
    <head>
      <title>Page Title</title>
    </head>
    <body>
      <p>Some text</p>
    </body>
  </html>
');

浏览器会以任何方式区别对待它们吗?

我可以在两者上设置 Cache-Control 和 gzip 压缩吗?

既然用的是express.js,为什么不用它的render函数来渲染

sendFile()send() 的不同之处在于它会根据文件扩展名的猜测自动为您设置 Content-Type header。

Can I set Cache-Control and gzip compression on both of them?

是的,下游发生的任何事情都独立于此处代码中发生的事情。

  • read an external index.html
  • parse it, append/update some tags on it

您应该为此使用 built-in 模板功能。另见:https://expressjs.com/en/api.html#res.render