Home > Web Front-end > JS Tutorial > How to install Node.js and start local service

How to install Node.js and start local service

php中世界最好的语言
Release: 2018-05-30 11:04:16
Original
3715 people have browsed it

This time I will bring you how to install Node.js and start local services. What are the precautions?The following is a practical case, let's take a look. .

1. Download the installation package:

Download address: https://nodejs.org/en/download /, download the corresponding windows 64-bit installation package according to the configuration of your computer. After the download is completed, install it.

2. Check whether the installation is successful

After the installation is completed, open the command line window and check whether the installation is successful, as shown in the figure below, type node -v displays the node.js version, and typing npm -v displays the npm version, indicating that both have been installed successfully.

3. Configure environment variables

Since my computer has installed node.js before, so You need to check whether the environment variable is configured. Open the command line and enter the command "path". You can see in the output that the environment variable already contains E:\nodejs\

4. Create An application

Before creating the application "Hello World", first understand what parts the node.js application consists of:

Introductionrequire d module: We can use the require directive to load Node.js modules.

Create server: The server can listen to client requests.

ReceiveRequest and responseRequest: The server is easy to create. The client can use a browser or terminal to send an HTTP request. The server returns the response data after receiving the request.

Let’s start creating the node.js application:

(1) Introduce the require module

We use the require directive to load the http module and assign the instantiated HTTP value to the variable http. The example is as follows:

var http = require("http");
Copy after login

(2) Create the server

Next We use the http.creatServer() method to create a server and use the listen() method to bind port 8080. The function receives and responds to data through request and response parameters. The example is as follows:

var http = require('http');
http.createServer(function (request, response) {
 // 发送 HTTP 头部 
 // HTTP 状态值: 200 : OK
 // 内容类型: text/plain
 response.writeHead(200, {'Content-Type': 'text/plain'});
 // 发送响应数据 "Hello World"
 response.end('Hello World\n');
}).listen(8080);
// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8080/');
Copy after login

Use the node command to execute the above code. The result is as follows:

Open the browser and visit http://localhost:8080/, you can See the web page with "Hello World" written on it, as shown below:

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

Recommended reading:

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

How to use JS Event binding, event flow model

The above is the detailed content of How to install Node.js and start local service. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template