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