Home >Web Front-end >JS Tutorial >NodeJS server creation and restart operation code sharing

NodeJS server creation and restart operation code sharing

php中世界最好的语言
php中世界最好的语言Original
2018-05-22 10:07:511587browse

This time I will share with you the nodeJS server creation and restart operation code. What are the precautions for the nodeJS server creation and restart operation? The following is a practical case, let's take a look.

1: First create a server.js file in the nodejs project, enter the following code

var http = require("http"); 
http.createServer(function(request, response) { 
 response.writeHead(200, {"Content-Type": "text/plain"}); 
 response.write("Hello World"); 
 response.end(); 
}).listen(8888);

and then enter the project path under cmd, Then enter node server.js to start the server, and then enter http://localhost:8888/ in the browser address bar to see the output Hello World on the interface. If we modify the value of Hello World and refresh the browser, we will find the output and There is no change. At this time, you need to manually restart the server to change the output. This is undoubtedly very annoying during the development process. It is best to have a script that can monitor all changed files. Once a file is found to be changed, the service will be restarted immediately. Reload the file you just modified.

Here is a recommendation: nodemon.

First of all, in order to make this command available globally, it is best for us to install it globally:

npm install -g nodemon
Then enter your project root directory: nodemon server.js

In this way, the application can be started, and the service will be automatically restarted after the file changes.

Two: ExitThe current terminal command output line command: Ctrl C twice.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

nodeJs anywhere case sharing of building a local server environment

NodeJs mobile phone access local server case analysis

The above is the detailed content of NodeJS server creation and restart operation code sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn