简体   繁体   English

tinylr / nodejs-如何访问当前运行的服务器

[英]tinylr/nodejs - how to access the currently running server

In the parent process, I have started the tiny-lr(livereload) server, followed by spawing a child process which looks for changes to the css files. 在父进程中,我启动了tiny-lr(livereload)服务器,然后生成一个子进程,该子进程查找对css文件的更改。 how to pass on the livereload server to the child process or is it possible to query for the livereload server that is currently running in the child process so that I don't create it again getting an already in use error for the port. 如何将livereload服务器传递给子进程,或者是否可以查询子进程中当前正在运行的livereload服务器,以便我不再创建它,而导致端口出现使用错误。

the same case with node http server. 与节点http服务器相同的情况。 can I know if the server is already running and use that instead of creating new one. 我可以知道服务器是否已经在运行,并使用它代替创建新服务器。

is it possible to query for the livereload - it is possible and may be implemented in more than one way. 是否可以查询livereload-这是可能的,并且可能以多种方式实现。

  1. Use stdout/stdin to communicate with the child process. 使用stdout/stdin与子进程进行通信。 For detailed description look HERE . 有关详细说明,请点击这里 Basically you can send messages from one process to the other and reply to them. 基本上,您可以将消息从一个进程发送到另一个进程并进行回复。

  2. Use http.request to check if the port is in use. 使用http.request检查端口是否正在使用。

  3. You can use a file: the process with the server keeps the file open in the write mode - the content of the file stores the port on which the server runs (if needed). 您可以使用文件: 服务器的进程使该文件在写模式下保持打开状态-文件的内容存储服务器运行所在的端口(如果需要)。

  4. You can use sockets for inter-process communication, as well. 您也可以使用套接字进行进程间通信。

Basically, none of the above guarantees 100% confidentiality, so you have to try/catch for errors anyway: the server may die just after your check, but before you wanted to do something with it. 基本上,以上方法都不保证 100%的机密性,因此无论如何您都必须try/catch错误:服务器可能会在检查之后但在您想对其进行处理之前就死机。

how to pass on the livereload server to the child process - if you mean sharing an object between different process that it is for sure out of question; 如何将livereload服务器传递给子进程 -如果要在不同进程之间共享对象,那肯定不可能的 if you mean changing the ownership of the object that I am some 99,99% sure it is not possible neither. 如果您的意思是更改对象的所有权,那么我有99,99%的把握是不可能的。

What is the problem with having just one process responsible for running the server? 仅由一个进程负责运行服务器有什么问题? And why not to use, let say, forever to take care of running and restarting the server, if needed? 而且,如果需要的话,为什么不永久使用它来运行和重新启动服务器呢?

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

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