Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http.createServer on('request') async/await functions

Trying to use an async function in request event from node.js http.createServer. I want to use vanilla node.js modules. I know I can use external dependencies but wish to solve without ideally.

I can't understand why I am getting the data back from async function as seen from console.logs yet the curl resp doesn't include it. I am not getting any errors, like writing headers after resp was sent back so confused why curl doesn't have body of { data: 1 }. If I remove async/await and hardcode the req.end to be 'test' its returned in curl request.

server.js

const server = http.createServer();
server.on('request', async (req, res) => {
  const data = await someAsyncFunc();
  console.log(req.url);
  console.log(data);
  res.end(JSON.stringify(data));
});

terminal

curl localhost:3000/test -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /test HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< Content-Length: 9
< Date: Tue, 17 Apr 2018 18:44:00 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
Not Found%

server.js log

/test
{ data: 1 }

----- UPDATE ------

Seems like the cause of this issue is I'm passing in a koa app.callback() to createServer, that seems to break the async stuff some how in request event for http server:

const app = new Koa();
http.createServer(app.callback());

I would have prefered to use server.on('request') but the only way I can get this to work is to use koa middleware.

like image 227
Labithiotis Avatar asked Jan 28 '23 06:01

Labithiotis


1 Answers

I cannot reproduce your issue.

I'm posting this as an answer just to share with you the exact code that I used to try to reproduce your problem to show you exactly what works fine for me.

There must be something in your actual code that is different than this that is causing an error or your curl request isn't actually connecting to the server that you think it is. You will have to share with us more info for us to be able help further. It's not an issue with the code you show in your question. Here's what I used that worked fine for me:

const http = require('http');

const server = http.createServer();
server.on('request', async (req, res) => {
  const data = await someAsyncFunc();
  console.log(req.url);
  console.log(data);
  res.end(JSON.stringify(data));
});

function someAsyncFunc() {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve({data: 1});
        }, 1000);
    });
}

server.listen(3000);

Then, I get this output:

curl localhost:3000/test -v
*   Trying ::1...
* TCP_NODELAY set
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /test HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.59.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 17 Apr 2018 23:53:32 GMT
< Connection: keep-alive
< Content-Length: 10
<
{"data":1}* Connection #0 to host localhost left intact
like image 132
jfriend00 Avatar answered Jan 30 '23 19:01

jfriend00