node. JS creates an HTTP server
If we use PHP to write back-end code, we need an Apache or Nginx http server, with MOD_PHP5 modules and php-cgi.
From this perspective, the entire "Receive HTTP request and provide WEB page" requirement does not need PHP to handle at all.
But for node. JS, the concept is completely different. When using node. js, we are not only implementing an application, but also implementing an entire HTTP server. In fact, our web application and the corresponding Web server are basically the same.
The underlying HTTP server
Create a file called Server.js in the root directory of your project and write the following code:
var http = require (' http '); Http.createserver (function (request, response) { Response.writehead ($, {' Content-type ': ' Text/plain '}); Response.End (' Hello world\n ');}). Listen (8888); Console.log (' Server running at http://127.0.0.1:8888/');
The above code we have completed a working HTTP server.
Use the node command to execute the above code:
node Server.jsserver running at http: // 127.0.0.1:8888/
Next, open the browser to access http://127.0.0.1:8888/, and you'll see a page with "Hello World".
Parsing 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 we 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.
node. JS creates an HTTP server