Writing the first node. JS app

Source: Internet
Author: User
Tags node server

Step1: Introduction of Required Module

First use the require directive to load the HTTP module and assign the instantiated HTTP value to the variable HTTP, as shown in the following example:

Step2: Creating a server

Next use the Http.createserver () method to create the server and bind port 8888 using the Listen method. The function receives and responds to data through the request, response parameter.

As an example, create a file called Server.js and write the following code:

Step3: Receiving requests and responding to requests

The above code completes an HTTP server that can work. The next step is to run the Server.js file on the command line. First Win+r, then enter cmd into the command prompt, and then continue the CD CD CD until you enter the root directory of Server.js, then run the Node Server.js command, and then you will see the following scenario:

Then enter the address in the browser address bar http://127.0.0.1:8888/Enter and you will see "Hello World"

The first node. JS application was successful.

Summarize:

1. Nodejs Installation: https://nodejs.org/en/download/(the version after Nodejs 0.6.0 is already available under Windows and comes with the NPM package Installation Manager. )

(starting node. js after installing Nodejs will open a black-like system command box, which is a command box that directly enters the JS code, so typing here node-v will prompt you to not have node's identifier)

When CMD enters the command prompt, enter NODE-V to see the version of node. JS installed. Enter Node-h to view Nodejs's help. You can also enter NPM-V to see if the NPM Nodejs Package Manager is already integrated.

2. Analyze 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, 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.

Writing the first node. JS app

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.