Javascript http.createServer on(';请求';)异步/等待函数

Javascript http.createServer on(';请求';)异步/等待函数,javascript,node.js,http,async-await,Javascript,Node.js,Http,Async Await,尝试在node.js http.createServer的请求事件中使用异步函数。我想使用vanilla node.js模块。 我知道我可以使用外部依赖项,但希望在没有理想情况下解决 我不明白为什么我要从console.logs中看到的异步函数返回数据,但是curl resp没有包含它。我没有收到任何错误,比如在resp被发回后写头,我很困惑为什么curl没有{data:1}的主体。如果我删除async/await并将req.end硬编码为“test”,它将在curl请求中返回 server.j

尝试在node.js http.createServer的请求事件中使用异步函数。我想使用vanilla node.js模块。 我知道我可以使用外部依赖项,但希望在没有理想情况下解决

我不明白为什么我要从console.logs中看到的异步函数返回数据,但是curl resp没有包含它。我没有收到任何错误,比如在resp被发回后写头,我很困惑为什么curl没有
{data:1}
的主体。如果我删除async/await并将req.end硬编码为“test”,它将在curl请求中返回

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));
});
终端

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%
-----更新------

似乎是这个问题的原因是我把一个膝关节炎应用程序.CALBACK()传递给CeaSereServer,这似乎打破了异步事件的一些东西:HTTP服务器的请求事件:

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

<>我宁愿使用Server。on(“请求”),但我能使它工作的唯一方法是使用膝关节炎中间件。< /P> < P>我不能复制您的问题。< /P> 我将此作为一个答案发布,只是为了与您分享我用来重现您的问题的确切代码,以向您展示什么适合我

您的实际代码中一定有与此不同的地方导致了错误,或者您的curl请求没有实际连接到您认为是的服务器。您必须与我们分享更多信息,以便我们能够进一步提供帮助。这与您在问题中显示的代码无关。以下是我用过的对我很有效的方法:

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);
然后,我得到这个输出:

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
curl localhost:3000/test-v
*正在尝试::1。。。
*TCP_节点集
*正在尝试127.0.0.1。。。
*TCP_节点集
*已连接到本地主机(127.0.0.1)端口3000(#0)
>获取/测试HTTP/1.1
>主机:localhost:3000
>用户代理:curl/7.59.0
>接受:*/*
>
什么是
someAsyncFunc
?您正在将结果打印到控制台,它不是来自请求。也许您应该在结束之前编写响应头:
res.writeHead(200,{“内容类型”:“您的内容MIME”})
(code
200
表示OK)看起来服务器没有这样的路由
GET/test
,甚至在代码执行之前,您就得到了404。。。。。。另外,如果
someAsyncFunc
花费的时间太长,客户端可能会出现连接超时错误。只要
someAsyncFunc()
返回解析为您的值的承诺,server.js中的代码就会显示为find。真的是这样吗?也许您应该向我们展示
someAsyncFunction()
的代码,因为这是发生更有趣事情的地方。所以我确实做了一些稍微不同的事情。我有一个膝关节炎应用程序,我正在将App. CalbEffter()传入CREATESEVER。我有这样做的理由,但这似乎是导致请求async无法正常工作的原因。
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