Step1: Introduction of Required Module
First use the require directive to load the HTTP module and assign the instantiated HTTP value to the variable HTTP, as shown in the following example:
Step2: Creating a server
Next use the Http.createserver () method to create the server and bind port 8888 using the Listen method. The function receives and responds to data through the request, response parameter.
As an example, create a file called Server.js and write the following code:
Step3: Receiving requests and responding to requests
The above code completes an HTTP server that can work. The next step is to run the Server.js file on the command line. First Win+r, then enter cmd into the command prompt, and then continue the CD CD CD until you enter the root directory of Server.js, then run the Node Server.js command, and then you will see the following scenario:
Then enter the address in the browser address bar http://127.0.0.1:8888/Enter and you will see "Hello World"
The first node. JS application was successful.
Summarize:
1. Nodejs Installation: https://nodejs.org/en/download/(the version after Nodejs 0.6.0 is already available under Windows and comes with the NPM package Installation Manager. )
(starting node. js after installing Nodejs will open a black-like system command box, which is a command box that directly enters the JS code, so typing here node-v will prompt you to not have node's identifier)
When CMD enters the command prompt, enter NODE-V to see the version of node. JS installed. Enter Node-h to view Nodejs's help. You can also enter NPM-V to see if the NPM Nodejs Package Manager is already integrated.
2. Analyze the HTTP Server for node. JS:
- The first line requests (require) node. js's own HTTP module and assigns it to the HTTP variable.
- Next, invoke the function provided by the HTTP module: Createserver. This function returns an object that has a method called Listen, which has a numeric parameter that specifies the port number that the HTTP server listens on.
Writing the first node. JS app