W
W
wlms2019-01-08 14:01:24
Node.js
wlms, 2019-01-08 14:01:24

How to execute two requests in a row?

I have validation functions. I moved them into separate functions and call them in route handlers.

// CHECK VALID URL
const checkValidUrl = async (req, res) => {}

// CHECK RIGHTS
const checkRights = async (req, res) => { }

Call:
// CHECK URL
router.post('/url', async(req, res) => checkValidUrl(req, res));

// CHECK RIGHTS 
router.post('/rights', async (req, res) => checkRights(req, res));

When I try to execute two functions in a row, I get an exception
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

I believe that when the first function is executed, the object req, res is already 'busy', and when the second one tries to take them, it is refused. Question. How to make two requests in a row. It can be combined into one function, where there will be "common" req, res, but how? I tried to complete each function through res.end (). Same error. Maybe you can somehow reset these objects after execution? Tell me what to do.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
de1m, 2019-01-08
@smlwmy

As far as I know, only one response is possible for one post coming from a client. Both should be included in the same answer.

D
Daniil Lebedinsky, 2019-01-08
@Lebed71

Apparently you have an error somewhere in the code of the handler functions, show what is there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question