【发布时间】:2022-09-29 20:29:09
【问题描述】:
我正在使用 Express 和 NodeJS 创建 API。但是当我完成编码时,我打开了索引,而不是渲染索引文件,它只是让我下载它。我找不到导致此问题的任何错误。这是我的代码:
app.get(\"/\", function(req,res){
res.render(\"index\", {name:auth.name})
}
app的定义是express(),auth是SQLite DBWrapper
我正在使用 Express 和 NodeJS 创建 API。但是当我完成编码时,我打开了索引,而不是渲染索引文件,它只是让我下载它。我找不到导致此问题的任何错误。这是我的代码:
app.get(\"/\", function(req,res){
res.render(\"index\", {name:auth.name})
}
app的定义是express(),auth是SQLite DBWrapper
res.render() 函数用于渲染视图并将渲染后的 HTML 字符串发送给客户端。但由于某种原因,您的代码正在下载。你能强制 MIME 类型为text/html 吗?尝试这个:
app.get("/", function (req, res) {
res.contentType("text/html");
res.setHeader("content-type", "text/html");
res.render("index", { name: auth.name });
});
【讨论】: