So far, we have learned a lot of things about the middleware system used in Express. Now, we will refresh that knowledge and create a practical application. We will create a middleware framework of our own that partly resembles the one found in Express.
All the files will be placed inside an app
folder, and the main entry point of the application will be named index.js
.
This file will contain an App
class similar to the one found in Express, with its constructor and the use
and handleRequest
methods.
Note
JavaScript doesn't have classes in the language yet, but we can simulate them by using a function as the constructor and adding other functions (methods) to the prototype.
The constructor will initialize the stack variable (that holds the middleware) to an empty array, and it will bind the handleRequest
function to the App
scope, as shown in the following code:
function App() { // allows us to call App() without using the `new` keyword if (!...