Express Tutorial 01-Getting Started tutorial classic Hello World

Source: Internet
Author: User

Directory:

Objective

First, Express? Nani?!

Ii. preparatory work before the start

Third, the test installation of the classic Hello World

Iv. using Express (1) to generate an application

V. Description

Objective:

This article is based on node. JS, so the reader needs to have a little bit of node. JS Basic knowledge.

Demo I followed the Windows operating system. Linux in fact the command is similar, you should also be able to find.

I also just learned this, so a lot of things are I from the major sites, and then attached to my practice and their own instructions.

If there is any mistake, I hope you can specify it in time, please forgive us.

First, Express? Nani?!

Express is a simple and flexible node. JS Web application framework that provides a range of powerful features to help you create Web apps. Express does not abstract two of features that are already in node. js, we just extend the functionality required for Web applications on top of it. Rich HTTP tools and middleware from the Connect framework come and go, creating robust, friendly APIs quickly and easily.

Ii. preparatory work before the start

  Step 1: First, we need to create a new folder in our own disk partition, and all of our operations will be done in this folder.

Here, I'm doing a demo on my D-plate.

Run cmd, start dos, enter "D:" To switch to the D drive, and then run "mkdir Nodejsdemo" to create a new folder named "Nodejsdemo":

  Step 2: in this folder, we have to create an "application package file", which is similar to. NET in Package.json, used for NuGet to manage file versions.

In our express, we also need to create Package.json This file, which is the same as other node packages, in which express as a dependency.

You can also use NPM Info Express version to get the latest release number of the express, preferably with the latest version number, so that the new features will not make you feel strange:

OK, we know that the latest Express version number is 4.12.2, then we can create Package.json this package file.

The Package.json file code is as follows:

{  "name": "Nodejsdemo",  "description": "Hello World Test App." ,  " Version ":" 0.0.1 ",  true,  " dependencies ": {    " Express ":" 3.x "   }}

  Step 3: Now that our Package.json file is ready, we can use NPM (1) to install the dependency, where the dependency is only express.

Run CD Nodejsdemo into our project directory, which is where we started the new, and also the Nodejsdemo folder where Package.json is located:

  Step 4: then we run the npm Install command to install the dependencies declared in Package.json:

  Step 5: when NPM is complete, Express 4.12.2 and its dependencies are installed in your "./node_modules" directory.

You can confirm by running the npm ls command that it will show express and its dependencies as the following tree structure:

Third, the test installation of the classic Hello World

In the first paragraph, our Express has been installed, below, we want to write the real code, but also test the installation results.

Step 1: In D:\NodejsDemo, which is the directory we created above, create a JS file: Man.js, as our main program.

The Main.js code is as follows:

var express = require (' Express ')var app = Express () app.get (function  (Request, response) {  response.send (' Hello world 'varfunction() {    console.log (' Listening on port%d ', server.address (). port);});

In the code above, we first introduce the Express module and then use Express () to create a new application.

In this application instance, you can use the app. VERB () defines the route, the example above is "GET/" returns the "Hello World" string.

  The request and response objects are consistent with what node native provides to you, and you can also perform response.pipe () , request.on (' data ', Callback) anything else that can be done without express.

We then use the Express object encapsulated method Response.send () , which returns the specified string and sets the Content-length.

Finally, we start monitoring 3000 and output the current listening information.

  Step 2: run cmd, start dos, go to the project directory, run the command "node main":

  Step 3: Open the browser, visit: http://127.0.0.1:3000:

Ah ah ah, every time you see these two words, are very excited there are wood ...

Iv. using Express (1) to generate an application

The Express team maintains an executable file that can quickly generate project templates, named Express (1).

If you use NPM to install the global express-generator, it can be accessed anywhere on your machine.

  Step 1: run cmd into dos, switch to D-disk, run the command npm install-g express-generator ,-G to indicate global overall. The effect is as follows:

  Step 2: This tool provides a very simple function to generate a skeleton of a program. But he also has limitations, for example, it only supports a very small number of template engines.

In fact, Express supports almost all of the template engines built for node. Check out the help with Express--help :

  Step 3: If you want to build a jade,stylus application, you just need to simply execute the following command: Express--session--css Stylus--ejs MyApp

It then creates a new MyApp site in our D drive, with the following structure:

d:\myapp│  app.js│  package.json│├─bin│      www│├─public│  ├─images│  ├─javascripts│  └─stylesheets│          style.styl│├─routes│      index.js│      users.js│└─views        error.ejs        Index.ejs

Step 4: As with other node programs, you must add Project dependencies:

d:\> cd Myappd:\myapp> NPM Install
D:\>CD Myappd:\myapp>NPM Installcookie[Email protected] node_modules\cookie-Parser├──[email protected]0.1.2└──cookie[Email protected][email protected]2.3.1Node_modules\ejs[email protected]2.1.2Node_modules\debug└──[email protected]0.7.0[email protected]1.5.1Node_modules\morgan├──basic[Email protected]├──[email protected]1.0.0└──on[Email protected] ([email protected]) Serve[Email protected] node_modules\serve-Favicon├──[email protected]0.2.4├──[email protected]0.7.0├──[email protected]1.3.0└──[email protected]1.5.1 ([email protected]) [email protected]4.12.2Node_modules\express├──merge[Email protected]├──cookie[Email protected]├──utils[Email protected]├──[email protected]0.1.2├──[email protected]0.2.4├──[email protected]1.1.1├──range[Email protected]├──escape[Email protected]├──content[Email protected]├──[email protected]1.0.0├──[email protected]0.3.3├──[email protected]1.3.0├──serve[Email protected]├──content[Email protected]├──path[Email protected]├──[email protected]1.0.0├──[email protected]2.3.3├──on[Email protected] ([email protected]) ├──[email protected]1.5.1 ([email protected]) ├──[email protected]0.12.1 ([email protected], [email protected], [email protected]) ├──proxy[Email protected] ([email protected], [email protected]) ├──[email protected]1.2.4 ([email protected], [email protected]) └──type[Email protected] ([email protected], [email protected]) Body[Email protected] node_modules\body-parser├──content[Email protected]├──raw[Email protected]├──[email protected]1.0.0├──[email protected]1.0.0├──[email protected]2.3.3├──on[Email protected] ([email protected]) ├──iconv[Email protected]└──type[Email protected] ([email protected], [email protected]) [email protected]0.42.3Node_modules\stylus├──css[Email protected]├──[email protected]0.3.5├──[email protected]0.5.8└──[email protected]3.2.11 ([email protected], [email protected]) D:\myapp>
Echo result of command run

  Step 5: Then, we can run it!!! Ha ha.

Run the command node app to start the main program, if the boot is unresponsive, you can also switch to the bin directory, run the node www launcher:

  Step 6: Open the browser, visit: http://127.0.0.1:3000:

  Step 7: Refresh the page, every request we make, Express will record:

V. Description:

This article is a simple introductory article, there is time later I will focus on the skeleton of the site system, the directory of mine-clearing type of explanation, and demonstrated two times custom development.

You can also look at this directory structure, manually modify the page, familiar with.

Express Tutorial 01-Getting Started tutorial classic 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.