Express provides rich HTTP tools for fast request processing and response. Core functions middleware, routing, based on three classes to achieve Router,Layer,Route.

1.Router

Main core contents:

  • Stack array stores child routes, middleware.
  • The Handle method loops through the stack array and executes the response callback function according to the rule.

Adding a route would normally write code like this:

	app.get('/list'.function (req, res) {
		res.end('hello express');
	});
Copy the code

The Router does one thing to support this notation:


	var methods = require('methods');  // Contains the HTTP method name
	
	methods.forEach((methods) = > {
		// Get a specific method name (methods)
		Router.prototype[methods] = function() {
			// Call route to push a route into the stack}})Copy the code

Express routing and middleware are placed in the stack array, so there is a sequencing problem. If you use a middleware, it needs to be placed in front of the routing.

	const express = require('express');
	const app = new express();
	
	app.use((req, res, next) = > {
	    console.log('111');
	    next();
	});
	
	app.get('/list',(req, res) => {
	    res.end('hello express');  
	});
	
	app.listen('8000');
Copy the code

Next means that the next middleware or routing can be executed. If an error occurs, an error can be thrown and handed to the error middleware for processing. The error middleware accepts four parameters, and the use of four parameters represents the error middleware. The first parameter is an error message

	app.use((req, res, next) = > {
	    console.log('111');
	    next(new Error('error'));
	});
	
	app.use((error, req, res, next) = > {
	    console.log(error);
	})
Copy the code

When the request is received, handle will be executed and stack will be looped. If the path and method are the same, the corresponding callback will be called. If the number of routes increases, the stack array will increase and the matching efficiency will decrease. In order to improve the efficiency, Express introduces route and Layer.

2.Layer

The same path is formed into a layer, match method is provided for matching judgment, and Handle method is used to implement in-depth matching methods of the current layer.

The simple idea is to match a path and then execute the callback method.

So when a route is registered, a new Layer instance is generated, and if there are other routes with the same path, it is placed in this Layer. How to put this layer, do it in Route.

3.Route

4. Achieve efficient matching

	app.route('/list').get((req, res) = > {
		res.end('hello get');  
	}).post((req, res) = > {
		res.end('hello post');  
	}).put((req, res) = > {
		res.end('hello put');
	}).delete((req, res) = > {
		res.end('hello delete');
	});
Copy the code

Here’s a pseudocode to see how to implement the app.route method:

	app.route = function(path) {
		const route = new Route(path); // Generate a route instance
		const Layer = new Layer(path, route.dispatch.bind(route)); // Use the dispatch method in route as the Layer handler
		
		this.stack.push(layer); // Put it in an array
		
		return route; // Return route instance for chain calls, deregister method
	}
Copy the code