使用 next() 和 res.send 。发送到客户端后无法设置标头。错误 [ERR_HTTP_HEADERS_SENT]:

问题描述 投票:0回答:3

我是 Node js 和 Express 的新手,正在学习基础知识,,在我看到的其他代码错误中,我曾经通过查看 throws 部分来进行调试,但它不是在这里..

编辑:我对中间件和 next() 的概念很陌生,这导致了错误,如已投票的答案中所示。

代码:

const express = require('express')
const app = express()

app.use('/add-product',(req,res,next)=>{
    console.log("This is add-product page")
    res.send("<form action='/product' method ='POST'><input type='text' name ='title'><button type ='submit'>Submit</button></form> ")
    next()
})
app.use('/product',(req,res,next)=>{
    console.log('this is product-page')
    res.redirect('/')
})
app.use('/', (req,res,next)=>{
    res.send('Welcome to NodeJS')
})
app.listen(3000)

错误:

[nodemon] starting `node app.js`
This is add-product page
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:387:5)
    at ServerResponse.setHeader (node:_http_outgoing:603:11)
    at ServerResponse.header (C:\Users\A\Desktop\vs code\the complete tutorial\node_modules\express\lib\response.js:794:10)
    at ServerResponse.contentType (C:\Users\A\Desktop\vs code\the complete tutorial\node_modules\express\lib\response.js:624:15)
    at ServerResponse.send (C:\Users\A\Desktop\vs code\the complete tutorial\node_modules\express\lib\response.js:149:14)
    at C:\Users\A\Desktop\vs code\the complete tutorial\app.js:14:9
    at Layer.handle [as handle_request] (C:\Users\A\Desktop\vs code\the complete tutorial\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\A\Desktop\vs code\the complete tutorial\node_modules\express\lib\router\index.js:328:13)
    at C:\Users\A\Desktop\vs code\the complete tutorial\node_modules\express\lib\router\index.js:286:9
    at Function.process_params (C:\Users\A\Desktop\vs code\the complete tutorial\node_modules\express\lib\router\index.js:346:12)
    
node.js express
3个回答
1
投票

当您完成处理请求、获取数据并想要发送最终响应时,请在 res.send() 之前使用

return

您面临的上述错误是由于您在

next()
中调用
'/add-product'
函数所致。它将检查下一个要调用的函数。

next()
函数用于在调用函数后希望另一个函数在同一路径上运行时。

试试这个:

app.use('/add-product', (req, res, next) => {
  console.log("This is add-product page")
  return res.send("<form action='/product' method ='POST'><input type='text' name ='title'><button type ='submit'>Submit</button></form>");
});

1
投票
app.use('/add-product',(req,res,next)=>{
    console.log("This is add-product page")
    res.send("<form action='/product' method ='POST'><input type='text' name ='title'><button type ='submit'>Submit</button></form> ")
    next()
})

在你

res.send(...)
之后你不能再这里了。当您想创建有条件的中间件时,请使用
next()
func。


0
投票

在向客户端发送响应时应始终使用 return。

const express = require('express')
const app = express()

app.use('/add-product',(req,res,next)=>{
    console.log("This is add-product page")
    return res.send("<form action='/product' method ='POST'><input type='text' name ='title'><button type ='submit'>Submit</button></form> ")
    
})
app.use('/product',(req,res,next)=>{
    console.log('this is product-page')
    return res.redirect('/')
})
app.use('/', (req,res,next)=>{
    return res.send('Welcome to NodeJS')
})
app.listen(3000)
© www.soinside.com 2019 - 2024. All rights reserved.