代码之家  ›  专栏  ›  技术社区  ›  user180574

如何在执行res.send()时设置内容类型?

  •  6
  • user180574  · 技术社区  · 6 年前

    在route函数的末尾,当我做这样的事情时。

    res.send(JSON.stringify({...}));
    

    我希望将响应的内容类型设置为“text/plain”,但它是“text/html”。有没有办法明确地设置它?我搜索了文件,但还没有线索。

    1 回复  |  直到 6 年前
        1
  •  6
  •   WilliamNHarvey    6 年前

    发送前设置标题: https://nodejs.org/api/http.html#http_response_setheader_name_value

    res.setHeader('content-type', 'text/plain');
    res.send(JSON.stringify({...}));