Express Middleware-01: Getting Started

Source: Internet
Author: User
Tags http authentication json

The middleware provides additional functionality between the point at which the request is received and when the request is formally processed to the sending response.

Express's Connect module provides a middleware framework that allows you to easily insert middleware functionality at the global or path level or to a single route.

Here are some of the middleware components supported by Express:

Middleware Description
Static Allows the express server to stream a GET request for a static file. This middleware is express built-in and it can be accessed via express.static ().
Express-logger Implement a formatted request logger to track requests to the server
Basic-auth-connect Provides support for basic HTTP authentication
Cookie-parser You can read the cookie from the request and set the cookie in the response
Cookie-session Provides cookie-based session support
Body-parser Resolves JSON data in the body of the POST request to the Rep.body property
Compression Provides gzip compression support for large responses to clients
Csurf Provides cross-site request forgery protection
1. Assign middleware to a path in the global scope

Specifies the middleware for all routes and implements the use () method on the Express app object. The syntax is as follows:

Use ([path],  middleware)

PATH variable : Optional, the default value is '/', which means all paths.
Middleware parameter : function, accepts three parameters, namely req, res, and next. Next is the next middleware function to be executed;

Each middleware component has a constructor, which returns the corresponding middleware functions;

var express = require (' Express ');
var bodyparser = require (' Body-parser ');
var app = Express ();

App.use ('/', Bodyparser ()); Assigning Body-parser middleware to all paths
2. Assigning middleware to a single path
var express = require (' Express ');
var bodyparser = require (' Body-parser ');
var app = Express ();

App.get ('/home ', bodyparser (), function (req, res) {
    res.send (' This request is logged ... ');
});

App.get ('/body ', function (req, res) {
    res.send (' This request is not logged. ');
});
3. Add multiple middleware functions
var express = require (' Express '),
    Bodyparser = require (' Body-parser '),
    Cookieparser = require (' Cookie-parser ') ),
    session = require (' express-session ');

var app = Express ();
App.use ('/', Bodyparser ())
   . Use (", Cookieparser ())
   . Use (", session ());

Attention:
The order in which the functions are allocated is the order in which they are applied in the request. Some middleware functions need to be added in front of other middleware functions. 4. Using Query middleware

The query middleware converts a queried string from a URL to a JavaScript object and saves it as the query property of the Request object.

Starting with the Express 4.x, this feature exists in the built-in Request parser without the need for additional middleware.

Here is the basic way to use query middleware:

var express = require (' Express ');
var app = Express ();

App.get ('/', function (req, res) {
    var id = req.query.id,
        score = Req.query.score;
    Console.log (Json.stringify (req.query));
    Res.send (' done ');
});

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.