JianghuJS-Custom Middleware
120021. Concept
- What is JianghuJS Middleware
In JianghuJS, the concept of middleware is similar to that in Egg.js. Middleware is a plugin mechanism that is added to the application through the app.middleware property. Each middleware is a function that takes two parameters: ctx (context object) and next (the next middleware). When an HTTP request is sent to the application, the middleware processes the request in the order it was added, then passes the request to the next middleware, until all middleware have been executed, ultimately producing a response that is returned to the client.
The main functions of middleware include:
Handling request and response logic: Middleware can execute logic before the request reaches the controller or before the response leaves the controller. This allows for processing between the request and response, such as permission validation, logging, etc.
Modifying the context object (ctx): Middleware can set and read the state and information of the request in the ctx object for use in subsequent middleware or controllers.
Calling the next middleware: By calling the next() function, middleware can pass the request to the next middleware. This way, a series of middleware forms a "pipeline" that executes in order.
Overall, middleware is a powerful tool for preprocessing requests and responses, handling common logic, and providing global functionality. In JianghuJS, developers can write custom middleware according to their needs to meet specific project requirements.
2. How to Create Custom Middleware
Configuration
- Configuration file:
/config/middlewareConfig.js - The configuration in
middlewareConfigwill be automatically merged into theconfigconfiguration, and the framework will automatically execute the corresponding middleware methods in the order of the following array;
- Configuration file:
Execution Order
module.exports.middleware = [
// cache
'prepareCache',
// /upload/*
'downloadUserInfo',
// /page/*
'pagePackage', 'pageUserInfo', 'pageAuthorization',
// /pageDoc/*
'pageDocPackage', 'pageDocUserInfo', 'pageDocAuthorization',
// /resource/*
'httpPackage', 'httpUserInfo', 'httpAuthorization', 'httpResourceHook'
]; - Execution Conditions
middlewareMatch determines whether to execute the middleware based on conditions; middleware not added to Match will execute by default;
module.exports.middlewareMatch = {
pagePackage: {
match(ctx) {
// URL format matches /appId/page/pageId
return ctx.request.method === 'GET'
&& ctx.request.path.startsWith(`/${ctx.app.config.appId}/page/`);
},
},
...
} - Creating Middleware
In the /app/middleware directory, each js file is a middleware, and the file name matches the middleware configuration above;
├── middlewareUtil
│ ├── packageUtil.js
│ └── userInfoUtil.js
├── downloadUserInfo.js
├── httpAuthorization.js
├── httpPackage.js
├── httpResourceHook.js
├── httpUserInfo.js
├── pageAuthorization.js
├── pageDocAuthorization.js
├── pageDocPackage.js
├── pageDocUserInfo.js
├── pageHook.js
├── pagePackage.js
├── pageUserInfo.js
├── socketAuthorization.js
├── socketPackage.js
├── socketPackageRecord.js
├── socketResourceAfterHook.js
├── socketResourceBeforeHook.js
├── socketUserInfo.js
├── xiaochengxuAuthorization.js
├── xiaochengxuPackage.js
├── xiaochengxuPackageRecord.js
├── xiaochengxuPageAuthorization.js
├── xiaochengxuPagePackage.js
├── xiaochengxuResourceAfterHook.js
├── xiaochengxuResourceBeforeHook.js
└── xiaochengxuUserInfo.js