I am trying to pipe my session api request through server in order to set an httpOnly cookie with session token, and ran into an error saying:
Can't set headers after they are sent
Not entirely sure what this means, but here is my interceptor in express, that listens to a post to /api/sessions
endpoint and in successful scenario sets a cookie
app.post('/api/sessions', (req, res) => {
const url = `${config.API_HOST}/sessions`
let apiCall = request.post(url, (err, response, body) => {
if (!err && response.statusCode === 200) {
const data = JSON.parse(body)
let sessionCookie = {
path: '/',
hostOnly: true,
secure: true,
httpOnly: true
}
res.cookie('SESS', data.token, sessionCookie)
}
})
req.pipe(apiCall).pipe(res)
})
EDIT: reason why I pipe it is to be able and use promisses in my client side app.