Create the first application in Nodejs

Source: Internet
Author: User
: 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:

  1. Introduce the required module: we can use the require command to load the Node. js module.

  2. Create a server: The server can listen to client requests, similar to HTTP servers such as Apache and Nginx.

  3. 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.

    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.