Search Docs
type ServerAPIs = { sockWrite: ( type: string, data?: string | boolean | Record<string, any>, ) => void; }; type SetupMiddlewares = Array< ( middlewares: { unshift: (...handlers: RequestHandler[]) => void; push: (...handlers: RequestHandler[]) => void; }, server: ServerAPIs, ) => void >;
undefined
提供执行自定义函数和应用自定义中间件的能力。
中间件的执行顺序是: unshift => 内置中间件 => push。
unshift
push
export default { dev: { setupMiddlewares: [ (middlewares, server) => { middlewares.unshift((req, res, next) => { next(); }); middlewares.push((req, res, next) => { next(); }); }, ], }, };
一些特殊场景需求可能需要使用服务器 API:
export default { dev: { setupMiddlewares: [ (middlewares, server) => { // 添加自定义 watcher 并在文件更新时触发页面刷新 watcher.on('change', (changed) => { server.sockWrite('content-changed'); }); }, ], }, };