为什么节点服务器必须在文件更改时重新启动? [英] Why node server has to be restart on file change?

查看:43
本文介绍了为什么节点服务器必须在文件更改时重新启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当文件更改时,为什么需要重新启动 NodeJS 服务器?其他服务器如 Apache、IIS、nginx 是否也需要这个,还是可以自动重启?

Why is it necessary to restart a NodeJS server when there is a file change? Do other servers like Apache, IIS, nginx require this as well or can they restart automatically?

推荐答案

你没有说你在谈论哪些文件,所以我会提到几个不同类型文件的问题.

You don't say which files you're talking about so I'll mention the issues with a couple different types of files.

对于构成 node.js 服务器的 Javascript 代码的 Javascript 文件,node.js 是一个持续运行的服务器.这意味着当服务器启动时,它会将您的 Javascript 代码解析到内存中,然后开始执行它.该服务器进程将一直运行,直到您将其停止.因为 node.js 是一个持续运行的服务器,如果你想更新组成服务器代码的 Javascript 文件,你必须停止服务器并重新启动它,让它加载并重新解析新更改的源文件.

For Javascript files that make up your Javascript code for your node.js server, node.js is a continuously running server. That means when the server starts up, it parses your Javascript code into memory and then starts executing it. That server process stays running until you stop it. Because node.js is a continuously running server, if you want to update the Javascript files that make up the server code, you have to stop the server and restart it to let it load and reparse the newly changed source files.

这与 PHP 和 Apache 之类的东西非常不同,后者为每个单独的请求从头开始运行给定的 PHP 脚本.由于没有长时间运行的 PHP 应用程序,并且每个 PHP 脚本都是针对每个请求从头开始的,因此它可以自动拾取新更改的 PHP 脚本,而无需重新启动 Apache 服务器.如果您有一个完全用 PHP 编写的长时间运行的服务器,那么它的行为可能与 node.js 类似.

This is very different than something like PHP with Apache that runs a given PHP script from scratch for each separate request. Since there is no long running PHP application and each PHP script is started from scratch for each request, then it can automatically pick up a newly changed PHP script without restarting the Apache server. If you had a long running server written entirely in PHP, then it would likely have similar behavior as node.js.

而且,如果您想升级您的 Apache 服务器代码,您必须重新启动 Apache(与 node.js 一样).

And, if you wanted to upgrade your Apache server code, you'd have to restart Apache (as with node.js).

您可以将 node.js 视为 Apache + PHP 合二为一,因为通常只有 node.js 才能满足两者的功能.它将 web 服务器功能与 webapp 逻辑集成在一起,而这些与 Apache + PHP 是分开的.

You can kind of think of node.js as Apache + PHP in one since the functions of both are generally met by just node.js by itself. It integrates the web server functionality with the webapp logic whereas those are separate with Apache + PHP.

对于由服务器提供并传送到浏览器的 HTML 文件或 Javascript 文件,您通常不必重新启动服务器,以便在后续浏览器请求中提供这些文件的新版本.但是,这在一定程度上取决于您使用的服务器框架以及它如何实现文件缓存.此行为并非特定于 node.js,而是内置于您用来通过 Web 服务器(例如 Express 或类似的东西)提供文件的任何代码中.

For HTML files or Javascript files that are served by the server and delivered to the browser, you will generally not have to restart the server for the new versions of those files to be served on subsequent browser requests. But, this depends a bit on which server framework you are using and how exactly it implements file caching. This behavior is not specific to node.js, but would be built into whatever code you were using to serve files by your web server (e.g. Express or something like that).

这篇关于为什么节点服务器必须在文件更改时重新启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆