M
M
MasterCopipaster2022-03-14 09:27:23
Node.js
MasterCopipaster, 2022-03-14 09:27:23

How to return control to the main function in nodejs when using http.createServer?

async function waitResolve() {
    http.createServer((request, response) => {
        if (request.method === 'GET') {
            response.end('success');
        } else {
            let body = '';
            request.on('data', chunk => {
                body += chunk.toString();
            });
            request.on('end', () => {
                // let params = parse(body);
                console.log(body);
                response.end('success');
                return Promise.resolve(body);
            });
        }
    }).listen(3000);
}
 
 
 
async function main() {
    await waitResolve();
}
 
main();


Hello everyone, tell me how, after receiving a request, return control to main and turn off the http server?
That is, the logic is simple, we started the http server, waited for an incoming request, received it, turned off the http server and returned the request body to the main function ... I thought to do it through Promise.resolve but it doesn't matter to him, he continues to work further ...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question