"Pro express.js" Learning notes--app.params Middleware

Source: Internet
Author: User

App.param middleware is used to obtain, process and output the parameters in the URL, and provide the public logic to achieve the purpose of code refactoring.

The following example takes three steps to refactor the code, and the App.param middleware works very clearly:

    1. Do not use middleware
    2. Working with custom middleware
    3. Using App.param Middleware
Do not use middleware
1varUsers = {
2' Azat ': {
3Email: ' [email protected] ',
4Website: ' Http://azat.co ',
5Blog: ' http://webapplog.com '
6}
7};
8
9varFinduserbyusername =function(username, callback) {
Tenif(!users[username]) {
OnereturnCallbackNewError (' No user matching ' + username) ');
A}
-
-returnCallbackNULL, Users[username]);
the}
-
-App.get ('/v1/users/:username ',function(Request, response, next) {
-varUsername = Request.params.username;
+Finduserbyusername (username,function(Error, user) {
-if(Error)returnNext (Error);
+returnResponse.render (' user ', user);
A});
at});
-
-App.get ('/v1/admin/:username ',function(Request, response, next) {
- varUsername = Request.params.username;
-Finduserbyusername (username,function(Error, user) {
-if(Error)returnNext (Error);
inreturnResponse.render (' admin ', user);
-});
to}); Working with custom middleware

1varFinduserbyusernamemiddleware =function(Request, response, next) {
2if(Request.params.username) {
3Console.log (' Username param was detected: ' + request.params.username ');
4Finduserbyusername (Request.params.username,function(Error, user) {
5if(Error)returnNext (Error);
6Request.user = user;
7returnNext ();
8})
9}Else{
TenreturnNext ();
One}
A}
-
-App.get ('/v2/users/:username ',
theFinduserbyusernamemiddleware,
-function(Request, response, next) {
-returnResponse.render (' user ', request.user);
-});
+
-App.get ('/v2/admin/:username ',
+Finduserbyusernamemiddleware,
Afunction(Request, response, next) {
atreturnResponse.render (' admin ', request.user);
-});View Code

Using App.param Middleware1App.param (' V3username ',function(Request,response,next,username) {
2Console.log (' Username param was detected: ' +username ');
3Finduserbyusername (username,function(Error,user) {
4if(Error)returnNext (Error);
5Request.user = user;
6returnNext ();
7});
8});
9
TenApp.get ('/v3/users/:v3username ',
One function(Request, response, next) {
AreturnResponse.render (' user ', request.user);
-});
-
theApp.get ('/v3/admin/:v3username ',
-function(Request, response, next) {
-returnResponse.render (' admin ', request.user);
-});View Code

Summary
    1. App.param middleware plays the role of AOP, unified interception of requests containing specified parameters, facilitating centralized maintenance and management;
    2. App.param Middleware simplifies the process of subsequent routing, without the need to explicitly call the middleware to deal with, more simple.

"Pro express.js" Learning notes--app.params Middleware

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.