Paddle To learn node. js, of course, you must first have node. JS environment (can go to the official website http://nodejs.cn/download installation), if still do not know how to configure the development environment, degrees Niang will tell you everything.
After the installation completes the environment, don't rush to develop. Before development, in order to get a clearer grasp of the code logic for our first application, we also need to look at the basic components of the node. JS application, as follows:
1. Introduction of the required module: we can use the Require directive to load the node. JS module.
2. Create server: The server can listen to the client's request, similar to Apache, Nginx and other HTTP server.
3. Receiving requests and responding to the request server is easy to create, and the client can send HTTP requests using a browser or terminal, and the server returns the response data after receiving the request.
Here, the development environment has been set up, the basic development ideas have also, then we can start the development of our first node. js application. Now that all programmers are in the process of learning a new development language, the first program is to output "Hello World", so let's create a node. JS application that outputs "Hello World".
Step One:
Create a JS script file to store our node. js code. In my case, I created a nodejs script called Helloworld.js.
Step Two:
Use the require directive to load the HTTP module (module API address:http://nodejs.cn/api/http.html) and assign the instantiated HTTP value to the variable HTTP, Examples are as follows:
// introduce HTTP Module const HTTP = require (' http ');
Step Three:
Use the Http.createserver () method to create the server and bind the 8080 port and IP address using the Listen method. The function uses the REQ, res parameter to receive and respond to data.
The associated APIs for using HTTP are as follows:
node. JS Code:
//define IP for listeningconst hostname = ' 127.0.0.1 ';//defining ports for listeningConst PORT = 8080;//Create a service//req used to accept client data//Res is used to send server data to clientsConst SERVER = Http.createserver ((req, res) = = { //Write header information to client after successful connectionRes.writehead, {' Content-type ': ' Text/plain '}); //body part, showing to clientRes.write (' Hello world ');res.end ();});//listening for IP addresses and port numbersServer.listen (port, hostname, () ={console.log (' server is running on http://${hostname}:${port}/');});
The above code can also be simply written as:
Const SERVER = Http.createserver (function(req,res) { res.writeheader (200,{' content-type ': ' Text/plain '}); Res.write (' Hello World '); Res.end ();}). Listen (8080, ' 127.0.0.1 ', () =>{console.log ("Server already running in http://127.0.0.1:8080")});
The above code we have completed a working HTTP server. Now we can look at the effect of the operation. Use the node command to execute the above code:
Node Helloworld.js
Browser Access Effects:
So far, our first application based on node. JS is done.
node. JS Learning Note (i)--Create your first app