简体   繁体   English

从javascript通过端口停止进程

[英]Stop a process by port from javascript

I'm using jest to write some tests for a node.js application.我正在使用 jest 为 node.js 应用程序编写一些测试。 I have a server server.js and a test file server.test.js .我有一个服务器server.js和一个测试文件server.test.js In my server.js I use the line在我的server.js我使用了这一行

    var secureServer = https.createServer(options, 
    app).listen(config.node_port, () => {
    logger.info("Service running on " + config.node_port)
    });

to start my server on port 8082. In server.test.js I use在端口 8082 上启动我的服务器。在server.test.js我使用

    var posClient = require('./pos-ClientJS.js');

to get access to the functions, that I have to test.以访问我必须测试的功能。 When I run my test I get the following console output:当我运行我的测试时,我得到以下控制台输出:

    Jest did not exit one second after the test run has completed.

    This usually means that there are asynchronous operations that weren't 
    stopped in your tests. Consider running Jest with `-- 
    detectOpenHandles` to troubleshoot this issue.

So my question is: Is there a way to stop the server running with javascript code like stop(8082) or sth.?所以我的问题是:有没有办法停止使用像stop(8082)或 sth. 这样的 javascript 代码运行的服务器? Or is there a less difficult way to solve this problem without stopping the process?或者有没有更简单的方法可以在不停止过程的情况下解决这个问题?

From the Nodejs HTTP module documentation, you can call secureServer.close() to stop listening at the specified port.从 Nodejs HTTP模块文档中,您可以调用secureServer.close()来停止在指定端口上的侦听。 If the module exposes the server to Jest, you can use the teardown methods to stop the server automatically after tests complete.如果模块将服务器暴露给 Jest,您可以使用拆卸方法在测试完成后自动停止服务器。

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

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