If we use PHP to write back-end code, we need Apache (XAMPP) 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.
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, the client can use the browser or terminal to send HTTP requests, the server receives the request and returns the response data
Create a file called Server.js in the root directory of your project and write the following code:
/***********************************************************/varHTTP = require (' http '); Http.createserver (function(Request, response) {//Send HTTP Header//HTTP Status value: 200:ok//content Type: Text/plainResponse.writehead, {' Content-type ': ' Text/plain '});//Send response data "Hello World"Response.End (' Hello world\n ');}). Listen (8888);//The terminal prints the following informationConsole.log (' Server running at http://127.0.0.1:8888/');/***********************************************************/
Into the node environment, into the more directory;
Input node server.js;
The carriage return will appear: Server running at http://127.0.0.1:8888/;
Copy the http://127.0.0.1:8888/to your browser's address bar and you'll see Hello world
Create the first Hello World with node. js