Create the first Hello World with node. js

Source: Internet
Author: User
Tags node server

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.