Node.js creates the first application
If we use PHP to write back-end code, we need an Apache or Nginx HTTP server, coupled with the mod_php5 module and php-cgi.
From this perspective, the entire need of "receiving HTTP requests and serving Web pages" does not require PHP to handle it at all.
But for Node.js, the concept is completely different. When using Node.js, we are not only implementing an application, but also an entire HTTP server. In fact, our web application and corresponding web server are basically the same.
Before we create the first "Hello, World!" application in Node.js, let us first understand what parts the Node.js application consists of:
Introducing the required module: We can use the require directive to load the Node.js module.
Create a server: The server can listen to client requests, similar to HTTP servers such as Apache and Nginx.
Receiving requests and responding to requests The server is easy to create. The client can use a browser or terminal to send HTTP requests, and the server returns response data after receiving the request.
Create a Node.js application
Step 1. Introduce the required 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:
<span>var</span><span> http </span><span>=</span><span>require</span><span>(</span><span>"http"</span><span>);</span>
Step 1. Create a server
Next we use the http.createServer() method to create a server and use the listen method to bind port 8888. Functions receive and respond to data through request and response parameters.
The example is as follows. Create a file called server.js in the root directory of your project and write the following code:
<span>var</span><span> http </span><span>=</span><span>require</span><span>(</span><span>'http'</span><span>);</span>
http.createServer(function(request , response){//Send HTTP header// HTTP status value: 200: OK// Content type: text/plain
response.onwritehead (200, {'Content-Type': 'text/plain'}); // Send the response data "Hello" hello World"
response
.end('Hello Worldn');}).listen(8888);// The terminal prints the following information
console
.log('Server running at http://127.0.0.1:8888/');With the above code we have completed a working HTTP server.
Use the node command to execute the above code:
<span>node server</span><span>.</span><span>js <br></span><span>Server</span><span> running at http</span><span>:</span><span>//127.0.0.1:8888/</span>
Next, open the browser and visit http://127.0.0.1:8888/, you will see a web page that says "Hello World".
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i
').text(i)); }; $numbering.fadeIn(1700); }); }); The above introduces the creation of the first application in Nodejs, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.