使用 sendFile() 在 Express 中发送静态文件

阅读 47

2022-07-27

Express ​​sendFile()​​​允许您发送原始文件作为对 HTTP 请求的响应。您可以将其​​res.sendFile()​​​ 视为单个端点的 Express ​​static​​中间件。

使用 ​​sendFile()​

假设您有一个如下所示的 HTML 文件​​index.html​​:

<h1>Hello World</h1>

通过将路径传递到 ​​index.html​​​,可以使用 ​​res.sendFile()​​ 使 Express 将此 HTML 文件作为 HTTP 响应提供。

注意:除非指定 ​​root​​ 选项,否则路径必须是绝对路径。

app.get('/', (req, res) => {
res.sendFile(`${__dirname}/index.html`)
})

如果不想指定绝对路径,可以通过 ​​root​​ 选项以指定路径所相对于的目录。

app.get('/', (req, res) => {
res.sendFile('index.html', { root: __dirname })
})

精彩评论(0)

0 0 举报