is looking at the source of Expressjs, first record the operating principle, the back if it is worth recording, will continue.
Expressjs is a layer of pipeline processing mechanism encapsulated on the Nodejs TCP listener. He can put the nodejs to the entire process of receiving a request to the response, and the pipeline is made up by using the. Use (Path,fun) method section.
The EXPRESSJS runs as follows:
1. After running bin/www
2. Build the App object: the object replicates all the members in the application module, also copies the Eventemitter members, and sets some default configurations
3. Encapsulate the middleware (or, optionally, a pipe) into a Layer object through App.use () and push to the stack array of the router object that has been assigned to the properties of the App object: _router.
4. Listening to TCP ports
5. Upon receipt of the request
6. Trigger the App object's handle (Req,res,next), which invokes the handle (Req,res,done) of the _rooter property, where it iterates through the stack array and takes out the middleware execution.
Note: When developing middleware, it is important to note whether next () is called, which is equivalent to the valve in the middleware and controls whether it is necessary to continue to flow to the next section of the pipeline.
Hope for valuable advice, thanks!
EXPRESSJS Operating principle