Nodejs Study of the 2:express version of Hello World

Source: Internet
Author: User

Then

1:express Installation of Nodejs learning

The results;

We continue to write the application.

Create a new App.js file

$ touch app.js

Copy the code in.

The meaning of this sentence is to introduce the ' express ' module and give it the ' express ' variable for use.var Express=Require' Express‘);Call the Express instance, which is a function that, when called without arguments, returns an express instance that assigns this variable to the app variable.Var app= Express ();The app itself has many methods, including the most commonly used get, post, Put/patch, delete, where we call the Get method and specify a handler function for our '/' path.This handler function will receive the REQ and res two objects, which are requested request and response respectively.The request contains various information from the browser, such as query, body, headers, etc., can be accessed through the Req object. /Res object, we generally do not take information from the inside, but through it to customize the information we output to the browser, such as header information, such as the content you want to output to the browser. Here we call its #send method, outputting a string to the browser. App.get ('/', function (req, Res) {res.Send (' Hello World');}); //Define the behavior of our app and let it listen to the local 3000 port. The second function here is a callback function that executes after the listen action succeeds, and here we execute a command-line output that tells us that the listener action is complete. App.listen (+, function () { console. log (' app is listening at Port');});



Perform

$ node app.js

At this point our app will run and the terminal will output app is listening at port 3000 . Then we open the browser, Access http://localhost:3000/ , will appear Hello World . If it does not appear, it must be the above step is wrong, debug yourself.

Nodejs Study of the 2:express version of Hello World

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.