1

I have a client side react app that is posting some information to a node express backend, and is awaiting a response on the client side. My sever side code is:

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');

})

On the client side I have

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
}

Currently the code is throwing error: 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)

Is it possible to send a response in POST? It is doing it naturally yes? What is the right API to call if I want to send a JSON back to the client?

xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88

1 Answers1

2

you need to

 return res.send() 

or

 return res.redirect()

to end execution on the server side

feiiiiii
  • 1,480
  • 1
  • 12
  • 27
  • ok when I do that I now get a response, however it is an error stating: SyntaxError: Unexpected end of input. Even though on the server side I get: "POST /api/verify-valid-email HTTP/1.1" 200 25 "http://localhost:3000/auth". (note I'm doing cor posting from two dev servers). When I do the exact same response in an `server.get(...` setting, I get the correct response on the cilent side. – xiaolingxiao Jun 13 '18 at 19:29
  • found the problem here: https://stackoverflow.com/questions/43317967/handle-response-syntaxerror-unexpected-end-of-input/43319482. It's becuase I set `no-cors` – xiaolingxiao Jun 13 '18 at 19:34
  • @chibro2 I didn't know about the no-cors mode on the client side, I was gonna say try to add cors on backend. I'm glad it worked for u! – feiiiiii Jun 13 '18 at 19:38