Home >Web Front-end >JS Tutorial >How to create and restart a nodeJS server

How to create and restart a nodeJS server

php中世界最好的语言
php中世界最好的语言Original
2018-05-30 11:06:011668browse

This time I will show you how to create and restart a nodeJS server, and what are the precautions for creating and restarting a nodeJS server. 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:

How to operate the default parameters of processing functions in ES5 and ES6 environments


How to operate $emit in vue Communicate with $on parent-child and sibling components

The above is the detailed content of How to create and restart a nodeJS server. 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