Used to extend the built-in Web Server of Modern.js, unlike Hook, Middleware can directly operate Node's origin request and response, and can be extended using the framework plugin.
In the next major release, Modern.js will use new middleware to replace this approach.
It is recommended to use UnstableMiddleware to handle page requests.
For more detail, see Extend Web Server.
Before using this API, please execute pnpm run new
to create a new "Custom Web Server" source code directory.
context
: Middleware context.
response
: provides a series of methods to process the response.request
: provides a series of methods to get request info.source
: provides Node.js native req
and res
object.next
: call next listener (not affect the server process, only current hook).The execution of the next
function does not affect built-in processes, only controls whether the next middleware executes. Rendering processes are interrupted only when the response is written.
Modern.js provides res.locals
to store local variables for the current request.
Middleware can also use runtime framework extensions like BFF.
When using framework runtime extensions, type information is exported from @modern-js/runtime/{namespace}
. Middleware can use framework syntax, such as framework middleware writing, the following is pseudo-code:
By default, the framework extension capability of Web Server is turned off after installing the framework extension plugin. If you want to use the framework extension, you can turn it on through 'server.enableFrameworkExt'.
The type name exported by the framework extension may not 'Middleware', but is named by the framework extension plugin.