Express Framework Learning Summary

Source: Internet
Author: User

Recently learned the express framework, in the course of learning, referring to some information, feel that the express framework is more useful than the original node. js. Below I will summarize the contents of my study as follows:

Express Chinese web http://www.expressjs.com.cn/

Express English web http://expressjs.com/

I. Express Framework

The Express framework is the node frame behind the scenes, so jquery, Zepto, Yui, Bootstrap are not a thing.

The popularity of express in the background, like jquery, is the de facto standard of business.

       Native node development, you will find a lot of problems. For example:         presenting a static page is inconvenient, need to handle each HTTP request, but also to consider 304 problem         routing code is not intuitive, you need to write a lot of regular expressions and string functions         can not focus on business, to consider a lot of other things

Installing the Express framework is the use of NPM commands
1   NPM Install--save Express

The--save parameter, which indicates that the Package.json file is automatically modified and the dependencies are added automatically.

Routing capabilities:

1    varExpress = require ("Express");2    3    varApp =Express ();4    5App.Get("/", Function (req,res) {6Res.send ("Hello");7    });8    9App.Get("/haha", Function (req,res) {TenRes.send ("This is the haha page, haha haha haha"); One    }); A     -App.Get(/^\/student\/([\d]{Ten})$/, Function (req,res) { -Res.send ("Student information, study number"+ req.params[0]); the    }); -     -App.Get("/teacher/:gonghao", Function (req,res) { -Res.send ("Teacher information, work No."+ req.params. Gonghao); +    }); -     +App.listen ( the);

static file servo capability:

1    App.use (Express.  Static("./public"));

Template engine:

1    varExpress = require ("Express");2    3    varApp =Express ();4    5App.Set("View Engine","Ejs");6    7App.Get("/", Function (req,res) {8Res.render ("haha",{9            "News": ["I'm a little news.","me too.","haha haha"]Ten        }); One    }); A     -App.listen ( the);

Second, the route

What to do when accessing a URL with GET request:

1    App.Get(" url ", Function (req,res) {2        3    });

When you use post to access a URL, do something:

1    App.post (" url ", Function (req,res) {2        3    });

if you want to process the request for any method of this URL, then write all

1    App.all ("/", Function () {2        3    });

Here's the URL, no case, that is, your route is

1    App.Get("/aab", Function (req,res) {2        res.send ("  Hello "); 3    });

Third, middleware

If there is no next parameter in my get, post callback function, then it will match the first route and it will not match down.

If you want to match down, then you need to write next ()

1App.Get("/", Function (req,res,next) {2Console.log ("1");3next ();4    });5    6App.Get("/", Function (req,res) {7Console.log ("2");8});

Routing get, post these things, is the middleware, the middleware order, matching on the first after the match will not be back. Next function can continue to match later.

App.use () is also a middleware. Unlike get, post, his URLs are not exactly matched. It can be expanded with small folders.

For example URL: http://127.0.0.1:3000/admin/aa/bb/cc/dd

1App.use ("/admin", Function (req,res) {2Res.write (Req.originalurl +"\ n");///ADMIN/AA/BB/CC/DD3Res.write (Req.baseurl +"\ n");///admin4Res.write (Req.path +"\ n");///AA/BB/CC/DD5Res.end ("Hello");6});

In most cases, the rendered content is Res.render () and will be rendered according to the template file in the views. If you do not want to use the Views folder, you want to set the folder name, then App.set ("views", "AAAA");

If you want to write a quick test page, of course you can use Res.send (). This function will automatically help us set the Content-type header and 200 status code based on the content. Send () can only be used once, as with end. It's not like end. Ability to set MIME types automatically.

If you want to use a different status code, you can:

Res.status (404). Send (' Sorry, we cannot find that! ');

If you want to use a different content-type, you can:

Res.set (' Content-type ', ' text/html ');

Iv. parameters for get requests and post requests

The parameters of the GET request are in the URL, and in native node, the URL module is used to identify the parameter string. In Express, there is no need to use the URL module. You can use the Req.query object directly.

Post requests are not directly available in Express and must use the Body-parser module. After use, the parameters can be obtained with req.body. However, if the form contains file uploads, you still need to use the formidable module.

Node is full of callback functions, so we encapsulate our own function, if there are asynchronous methods, such as I/O, then the callback function is used to encapsulate the method.

Express Framework Learning Summary

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.