简体   繁体   English

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

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

Trying to use an async function in request event from node.js http.createServer. 尝试在node.js http.createServer的请求事件中使用异步函数。 I want to use vanilla node.js modules. 我想使用vanilla node.js模块。 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. 我无法理解为什么我从console.logs中看到异步函数返回数据,但curl resp不包含它。 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 } . 我没有收到任何错误,比如在resp被发回之后编写标题这么混淆为什么curl没有{ data: 1 }主体。 If I remove async/await and hardcode the req.end to be 'test' its returned in curl request. 如果我删除async / await并将req.end硬编码为'test',则返回curl请求。

server.js 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 server.js日志

/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: 似乎这个问题的原因是我在koa app.callback()中传递给createServer,这似乎打破了async的一些如何在http服务器的请求事件中:

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. 我本来希望使用server.on('request'),但我能让它工作的唯一方法是使用koa中间件。

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. 实际代码中必定存在与此不同的内容,导致错误或您的curl请求实际上并未连接到您认为的服务器。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM