: This article describes how to create the first application in Nodejs. For more information about PHP tutorials, see. Create the first application in Node. js
If we use PHP to write back-end code, we need Apache or Nginx HTTP server with mod_php5 module and php-cgi.
From this perspective, PHP is not required to handle the entire "receive HTTP requests and provide Web pages" requirement.
But for Node. js, the concept is completely different. When using Node. js, we not only implement an application, but also implement the entire HTTP server. In fact, our Web applications and their corresponding Web servers are basically the same.
Create the first "Hello, World!" in Node. js !" Before using the Node. js application, let's first understand the components of the Node. js application:
Introduce the required module: we can use the require command 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.
The server that receives and responds to requests is easy to create. the client can send HTTP requests using a browser or terminal, and the server returns response data after receiving the requests.
Create a Node. js application
Step 1. introduce the required module
We use the require command to load the http module and assign the instantiated HTTP value to the variable http. The example is as follows:
var http =require("http");
Step 1. create a server
Next, we use http. createServer () to create a server and use the listen method to bind port 8888. The function receives and responds to data through the request and response parameters.
The example is as follows. create a file named server. js under the root directory of your project and write the following code:
var http =require('http');
Http. createServer (function (request, response) {// send HTTP header // HTTP status value: 200: OK // Content Type: text/plain
Response. writeHead (200, {'content-type': 'Text/plain '}); // send response data "Hello World"
Response. end ('Hello World \ n') ;}). listen (8888); // The terminal prints the following information.
Console. log ('server running at http: // 127.0.0.1: 8888 /');
The above code completes an HTTP server that can work.
Run the preceding code using the node command:
node server.js
Server running at http://127.0.0.1:8888/
Next, open the browser to access http: // 127.0.0.1: 8888/, and you will see a webpage with "Hello World.
'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
'). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script
The above describes how to create the first application of Nodejs, including the content, and hope to help those who are interested in the PHP Tutorial.