首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节点express.js响应post请求

节点express.js响应post请求
EN

Stack Overflow用户
提问于 2018-06-13 18:44:11
回答 1查看 3.4K关注 0票数 1

我有一个客户端react应用程序,它将一些信息发布到node express后端,并且正在等待客户端的响应。我的服务器侧代码是:

代码语言:javascript
复制
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');

})

在客户端我有

代码语言:javascript
复制
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

代码语言:javascript
复制
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)

是否可以在邮件中发送回复?它是自然做的,是吗?如果我想将JSON发送回客户端,正确的API是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-13 18:46:06

你需要

代码语言:javascript
复制
 return res.send() 

代码语言:javascript
复制
 return res.redirect()

在服务器端结束执行

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50844175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档