【问题标题】:Why does my NodeJS Server make clients download the files?为什么我的 NodeJS 服务器让客户端下载文件?
【发布时间】: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

    标签: node.js express


    【解决方案1】:

    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 });
    });
    

    【讨论】:

    • 我使用的是哈巴狗而不是 html 引擎
    猜你喜欢
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2022-01-13
    • 2017-02-19
    • 2016-10-23
    • 2019-07-08
    相关资源
    最近更新 更多