Hello everyone, I am Pipi, today to share some fun ~

preface

Nodejs is a JavaScript scripting language for the GoogleV8 engine, which is essentially a server that can parse web content and produce results. It’s a great addition to JavaScript, and Node executes faster than JavaScript and supports distribution because it uses an event-driven, non-blocking model. It’s boring to talk too much. Why don’t we build our own.

Download Nodejs

Because xiaobibian is Win7 system, the new version of Nodejs does not support, do not know why? It is very Win8 friendly. So xiaobian in Tsinghua mirror download down, to share with you. There are two versions. One is the installation package version (with the suffix.msi) without setting environment variables. The other is a zip package, directly decompress and configure the environment variables. Here xiaobian suggest you download the installation package, considering that some people do not understand how to set the environment variable, or install the package better point, xiaobian steal a lazy installation package.

Install Nodejs

First of all, we need to agree with its user agreement, and then choose the installation path. There is no hard and fast rule that it must be placed on the system disk, so please put it on another disk, as follows:

Then click Next and you’ll see the environment Settings and install dependencies. Forget it. Next continues as follows:

Then we see that there is an option, as shown:

This is to ask us whether to choose this tool or not. If we choose it, we will download it. It is suggested to select it and wait for a moment.

A CMD window will appear telling us to download the tool and close all the Windows, as shown in the following figure:

Enter CMD to obtain the Node version number. If the command output indicates that the installation is successful, as follows:

3. New projects

First create a folder with no Chinese or capital letters, then open a command prompt in that directory and type the command “NPM init” as follows:

To create a Node project, press Enter and type Yes. The configuration given above is for the project. Open the folder to have a look, as shown below:

A Json file is created. Then we need to create a Static library for this project. We usually put images, HTML, etc. We call it Static, and then we put an HTML file called “index.html”. Then we need to create a Json file in the Json file directory, which can be used to start the server.

Then we access it. In this case, the Js file is the server program.

Build a server that can be accessed in a browser

The server can only be accessed in Node, not in the browser. Create a server Js file with the following contents:

var http = require('http'); var server = http.createServer(function (req,res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); Server. Listen (8080, '127.0.0.1); The console. The log (' Server running on http://127.0.0.1:8080/ ');Copy the code

Then we start the service in CMD, as follows:

Success.

Five, the summary

After watching Nodejs build server is not feel very easy, don’t worry, more exciting is still waiting for you, this is just the beginning.