EJS错误[err_http_headers_sent]:将标题发送到客户端后无法设置标头

发布于 2025-02-06 21:42:15 字数 3380 浏览 3 评论 0原文

IM正在学习EJS并遇到此错误,同时尝试将响应发送到视图中,并在浏览器上获得标签响应,例如H1> a<%= kindofday%> gt;呢未显示标头形式的结果,它只会给字符串响应带有错误 错误[ERR_HTTP_HEADERS_SENT]:将标题发送到客户端时,无法设置标头

,我的代码表示

const express = require('express')
const bodyparser = require("body-parser")
const { send } = require('process')
const app = express()
const port = 3000
app.set("view engine", "ejs")

app.get('/', (req, res) => {
    let today = new Date()
    let day = "";
    if (today.getDay() === 6 || 0) {
        day = "weekend"
    } else {
        res.send("work day")
    }
    res.render("list", { kindOfDay: day })
        //res.send('Hello World!')
})

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

此处是视图代码,

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>to do list</title>
</head>

<body>
    <h1>its a
        <%=kindOfDay%> !</h1>
    <p>its a
        <%=kindOfDay%> !</p>
</body>

</html>

这是我一直收到的错误消息

Example app listening at http://localhost:3000
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:372:5)
    at ServerResponse.setHeader (node:_http_outgoing:576:11)
    at ServerResponse.header (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:794:10)
    at ServerResponse.send (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:174:12)
    at done (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:1035:10)
    at tryHandleCache (C:\Users\zahab\Desktop\todolist-v1\node_modules\ejs\lib\ejs.js:280:5)
    at View.exports.renderFile [as engine] (C:\Users\zahab\Desktop\todolist-v1\node_modules\ejs\lib\ejs.js:491:10)
    at View.render (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\application.js:657:10)
    at Function.render (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\application.js:609:3)
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:372:5)
    at ServerResponse.setHeader (node:_http_outgoing:576:11)
    at ServerResponse.header (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:794:10)
    at ServerResponse.contentType (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:624:15)
    at ServerResponse.send (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:149:14)
    at done (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:1035:10)
    at tryHandleCache (C:\Users\zahab\Desktop\todolist-v1\node_modules\ejs\lib\ejs.js:280:5)
    at View.exports.renderFile [as engine] (C:\Users\zahab\Desktop\todolist-v1\node_modules\ejs\lib\ejs.js:491:10)
    at View.render (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\application.js:657:10)

i m learning ejs and ran into this error while trying to send get response into a view and get the tag responce on browser like h1>its a<%=kindOfDay%> ! not showing result in header form it just gives the string responce with an error
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

heres my code express

const express = require('express')
const bodyparser = require("body-parser")
const { send } = require('process')
const app = express()
const port = 3000
app.set("view engine", "ejs")

app.get('/', (req, res) => {
    let today = new Date()
    let day = "";
    if (today.getDay() === 6 || 0) {
        day = "weekend"
    } else {
        res.send("work day")
    }
    res.render("list", { kindOfDay: day })
        //res.send('Hello World!')
})

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

here is the view code

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>to do list</title>
</head>

<body>
    <h1>its a
        <%=kindOfDay%> !</h1>
    <p>its a
        <%=kindOfDay%> !</p>
</body>

</html>

this is the error message i keep getting

Example app listening at http://localhost:3000
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:372:5)
    at ServerResponse.setHeader (node:_http_outgoing:576:11)
    at ServerResponse.header (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:794:10)
    at ServerResponse.send (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:174:12)
    at done (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:1035:10)
    at tryHandleCache (C:\Users\zahab\Desktop\todolist-v1\node_modules\ejs\lib\ejs.js:280:5)
    at View.exports.renderFile [as engine] (C:\Users\zahab\Desktop\todolist-v1\node_modules\ejs\lib\ejs.js:491:10)
    at View.render (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\application.js:657:10)
    at Function.render (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\application.js:609:3)
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:372:5)
    at ServerResponse.setHeader (node:_http_outgoing:576:11)
    at ServerResponse.header (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:794:10)
    at ServerResponse.contentType (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:624:15)
    at ServerResponse.send (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:149:14)
    at done (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\response.js:1035:10)
    at tryHandleCache (C:\Users\zahab\Desktop\todolist-v1\node_modules\ejs\lib\ejs.js:280:5)
    at View.exports.renderFile [as engine] (C:\Users\zahab\Desktop\todolist-v1\node_modules\ejs\lib\ejs.js:491:10)
    at View.render (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\zahab\Desktop\todolist-v1\node_modules\express\lib\application.js:657:10)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤者何惧 2025-02-13 21:42:15

我有这个错误,因为我忘了添加返回语句。因此,您可能需要仔细检查在适用的情况下

app.get('/', (req, res) => {
    let today = new Date()
    let day = "";
    if (today.getDay() === 6 || 0) {
        day = "weekend"
    } else {
        return res.send("work day")   // add a return statement
    }
    return res.render("list", { kindOfDay: day })   // add a return statement
        //res.send('Hello World!')
})

I had this error because I forgot to add a return statement. So you might want to double-check that where applicable:

app.get('/', (req, res) => {
    let today = new Date()
    let day = "";
    if (today.getDay() === 6 || 0) {
        day = "weekend"
    } else {
        return res.send("work day")   // add a return statement
    }
    return res.render("list", { kindOfDay: day })   // add a return statement
        //res.send('Hello World!')
})

梦途 2025-02-13 21:42:15

查看此部分,您已经发送了一个res.send(“工作日”),然后在“ else”之外再次尝试发送一个新元素res.render(“ list”,{binkofday:day}),那就是该问题“将标题发送到客户端后无法设置标头”,因为您已经发送并尝试了新的。如果您评论此部分并运行将起作用。因此,您需要在此特定部分更改逻辑编程。端点中的回调函数'/'需要仅返回一个上下文作为响应。

const express = require('express')
const bodyparser = require("body-parser")
const { send } = require('process')
const app = express()
const port = 3000
const path = require('path')

app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set("view engine", "ejs")

app.get('/', (req, res) => {
    let today = new Date()
    let day = "";
    if (today.getDay() === 6 || 0) {
        day = "weekend"
    } else {
        res.status(200).send("work day")
    }
    //res.render("list", { kindOfDay: day })
        //res.send('Hello World!')
})

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

Look at this part, you already sent a res.send("work day") and after outside the "else" again you are trying to send a new element res.render("list", { kindOfDay: day }), that's the problem 'Cannot set headers after they are sent to the client', because you already sent and trying a new. If you comment this part and run will work. So, you need to change your logical programming in this specific part. Your callback function in your endpoint '/' needs to return just one context as a response.

const express = require('express')
const bodyparser = require("body-parser")
const { send } = require('process')
const app = express()
const port = 3000
const path = require('path')

app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set("view engine", "ejs")

app.get('/', (req, res) => {
    let today = new Date()
    let day = "";
    if (today.getDay() === 6 || 0) {
        day = "weekend"
    } else {
        res.status(200).send("work day")
    }
    //res.render("list", { kindOfDay: day })
        //res.send('Hello World!')
})

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文