简体   繁体   English

在nodejs中运行http请求以测试我的rest api时,它停止了,为什么?

[英]When running http requests in nodejs to test my rest api it stops, why?

I have a rest api working perfectly and now instead of manually testing it with postman I have written http requests in order to test CRUD operations. 我有一个REST API可以正常工作,现在我不是用邮递员手动对其进行测试,而是编写了HTTP请求以测试CRUD操作。 I know for a fact that all the requests are working as they give me status codes of 200. 我知道事实是,所有请求都可以正常工作,因为它们的状态代码为200。

My problem is when running the tests command line will show it ran through nearly all of them but it will not go beyond some of the GET requests (even though I have the same code for requests of the same type earlier in the code. 我的问题是,在运行tests命令行时,它将显示它几乎遍历了所有命令,但它不会超出某些GET请求(即使我在代码前面针对相同类型的请求使用了相同的代码)。

When I comment out the GET request where it gets stuck, it runs the requests after that one with no problem. 当我注释掉卡住的GET请求时,它会在那之后毫无问题地运行请求。

This is the code I have for my GET requests: 这是我用于GET请求的代码:

var options = {
    host: 'localhost',
    port: 4000,
    path: '/api/services/00001/method/01/args/01'
};
http.get(options, function(res) {
   console.log("Got response: " + res.statusCode+"\nGET argument 01 for user 00001 method 01.\n\n ");
}).on('error', function(e) {
   console.log("Got error: " + e.message);
});

Am I missing something that will occasionally get the code to stop running? 我是否缺少一些偶尔会导致代码停止运行的东西? Any help is appreciated. 任何帮助表示赞赏。

I can add code for my other operations as well if needed(POST, PUT, DELETE). 如果需要的话,我也可以为其他操作添加代码(POST,PUT,DELETE)。

After running my entire code with nothing commented out, I realized that it also stops in my second delete request, code is as follows: 在运行完所有代码且没有任何注释后,我意识到它也会在第二个删除请求中停止,代码如下:

var options = {
   host: 'localhost',
   port: 4000,
   path: '/api/services/00001/method/01',
   method: 'DELETE'
};

http.get(options, function(res) {
  console.log("Got response: " + res.statusCode +"\nDELETE method 01 for user 00001.\n\n ");
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

The solution could be one or both of: 解决方案可以是以下一项或两项:

  • Setting agent: false in your request options . 设置agent: false在您的请求options agent: false This prevents the request from using the global http agent socket pool, meaning the request will get its own new socket every time. 这样可以防止请求使用全局http代理套接字池,这意味着请求每次都会获得自己的新套接字。 I have seen strange things where requests never start because of node's http agent implementation. 我已经看到了奇怪的事情,由于节点的http代理实现,请求从未启动。 The one thing you should be aware of though is your OS user's (the one the process is running under) file descriptor limits. 但是,您应该知道的一件事是OS用户(文件正在运行的进程)的文件描述符限制。 If you do a lot of concurrent requests using agent: false , you may need to up that OS limit. 如果您使用agent: false进行大量并发请求,则可能需要提高该操作系统限制。

  • Use res.resume() in your request callback to drain any response data from the server whenever you don't care about it. 每当您不关心它时,在请求回调中使用res.resume()即可耗尽服务器中的所有响应数据。 Doing so may ensure that a socket from the http agent socket pool is available for use by another request. 这样做可以确保来自HTTP代理套接字池的套接字可供其他请求使用。

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

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