节点 express.js 响应 post 请求
Node express.js respond to post request
我有一个客户端 react
应用程序正在向 node
express
后端发布一些信息,并且正在等待客户端的响应。我的服务器端代码是:
server.post('/api/verify-valid-email', (req : express.Request, res :
express.Response, next) => {
const body = JSON.parse(req.body)
if (body && body.email) {
const email = body.email
res.set('Content-Type', 'application/JSON')
res.send(JSON.stringify({ validEmail: true }))
} else {
res.set('Content-Type', 'application/JSON')
res.send(JSON.stringify({ validEmail: false }))
}
res.redirect('/home');
})
在客户端我有
callApi = async () => {
const res = await fetch( '/api/verify-valid-email, {
method : 'POST',
headers : {
'Accept' : 'application/JSON',
'Content-Type': 'application/JSON',
},
mode: 'no-cors',
body: JSON.stringify({
email: 'xxx@gmail.com'
})
})
const body = await res.json();
return body
}
当前代码抛出错误:can't set headers after they are set
:
error: _http_outgoing.js:356
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (/Users/lingxiao/Documents/Career/spice.ai/server/functions/node_modules/express/lib/response.js:767:10)
at Object.email_verifier.verify [as callback] (/Users/lingxiao/Documents/Career/spice.ai/server/functions/lib/index.js:42:21)
at callback (/Users/lingxiao/Documents/Career/spice.ai/server/node_modules/email-verify/index.js:154:19)
at Socket.<anonymous> (/Users/lingxiao/Documents/Career/spice.ai/server/node_modules/email-verify/index.js:239:5)
at Socket.g (events.js:292:16)
at emitNone (events.js:91:20)
at Socket.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
是否可以在 POST 中发送响应?它是自然而然地做的吗?如果我想将 JSON 发送回客户端,正确的 API 调用是什么?
你需要
return res.send()
或
return res.redirect()
在服务器端结束执行
我有一个客户端 react
应用程序正在向 node
express
后端发布一些信息,并且正在等待客户端的响应。我的服务器端代码是:
server.post('/api/verify-valid-email', (req : express.Request, res :
express.Response, next) => {
const body = JSON.parse(req.body)
if (body && body.email) {
const email = body.email
res.set('Content-Type', 'application/JSON')
res.send(JSON.stringify({ validEmail: true }))
} else {
res.set('Content-Type', 'application/JSON')
res.send(JSON.stringify({ validEmail: false }))
}
res.redirect('/home');
})
在客户端我有
callApi = async () => {
const res = await fetch( '/api/verify-valid-email, {
method : 'POST',
headers : {
'Accept' : 'application/JSON',
'Content-Type': 'application/JSON',
},
mode: 'no-cors',
body: JSON.stringify({
email: 'xxx@gmail.com'
})
})
const body = await res.json();
return body
}
当前代码抛出错误:can't set headers after they are set
:
error: _http_outgoing.js:356
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (/Users/lingxiao/Documents/Career/spice.ai/server/functions/node_modules/express/lib/response.js:767:10)
at Object.email_verifier.verify [as callback] (/Users/lingxiao/Documents/Career/spice.ai/server/functions/lib/index.js:42:21)
at callback (/Users/lingxiao/Documents/Career/spice.ai/server/node_modules/email-verify/index.js:154:19)
at Socket.<anonymous> (/Users/lingxiao/Documents/Career/spice.ai/server/node_modules/email-verify/index.js:239:5)
at Socket.g (events.js:292:16)
at emitNone (events.js:91:20)
at Socket.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
是否可以在 POST 中发送响应?它是自然而然地做的吗?如果我想将 JSON 发送回客户端,正确的 API 调用是什么?
你需要
return res.send()
或
return res.redirect()
在服务器端结束执行