{"id":27111198,"url":"https://github.com/alanwei43/express-middleware-chain-response","last_synced_at":"2026-04-11T06:03:46.689Z","repository":{"id":37884092,"uuid":"209914861","full_name":"alanwei43/express-middleware-chain-response","owner":"alanwei43","description":"express middleware for chain response","archived":false,"fork":false,"pushed_at":"2023-01-04T10:58:48.000Z","size":1988,"stargazers_count":0,"open_issues_count":22,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-03T09:45:22.654Z","etag":null,"topics":["express","express-middleware","webpack","webpack-dev-server"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alanwei43.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-21T02:50:10.000Z","updated_at":"2019-10-08T01:34:33.000Z","dependencies_parsed_at":"2023-02-02T09:32:46.202Z","dependency_job_id":null,"html_url":"https://github.com/alanwei43/express-middleware-chain-response","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanwei43%2Fexpress-middleware-chain-response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanwei43%2Fexpress-middleware-chain-response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanwei43%2Fexpress-middleware-chain-response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alanwei43%2Fexpress-middleware-chain-response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alanwei43","download_url":"https://codeload.github.com/alanwei43/express-middleware-chain-response/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247574065,"owners_count":20960495,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express","express-middleware","webpack","webpack-dev-server"],"created_at":"2025-04-07T00:56:49.106Z","updated_at":"2025-12-30T23:06:23.981Z","avatar_url":"https://github.com/alanwei43.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Express 中间件\n\n主要用于 webpack-dev-server mock接口使用.\n\n## 安装\n\n```bash\nnpm install @alanlib/express-middleware-chain-response\n```\n\n## 使用方式\n\n函数定义\n\n```javascript\n/**\n * 获取 express 中间件\n * @param {Array.\u003cstring | {isOpen: boolean, isMatch: function(): Promise\u003cboolean\u003e getResponse: function(): Promise}\u003e} modules 模块(可以是已经加载好的模块数组, 也可以是模块文件所在目录, 或者模块的文件路径)\n * @param {{debug: boolean, switchPath: string, currentSwitchOn: boolean}} options 选项\n * @returns {function} express中间件\n */\nfunction chainResponse(modules, options){\n    //...\n}\n```\n\n* options: \n    * debug: `boolean` 开启会输出更多log日志\n    * switchPath: `string` 用来开启/关闭此中间件的拦截功能, 默认 `/@alanlib/express-middleware-chain-response`\n    * currentSwitchOn: `boolean` 默认开启/关闭拦截功能\n\n可以指定模块所在目录: `chainResponse([\"../modules\"])`, 或模块文件: `chainResponse([\"../modules/jsonp-module.js\"])`\n\n也可以直接传入模块: `chainResponse([require(\"./module1\"), require(\"./module2\")])`\n\n或者混合使用: `chainResponse([\"../modules-dir\", \"./module1.js\", require(\"./module1\"), require(\"./module2\")])`\n\n### webpack-dev-server\n\n参考例子: [samples/webpack-dev-sever-sample](./samples/webpack-dev-sever-sample)\n\n```javascript\nconst { chainResponse } = require(\"express-middleware-chain-response\");\n\nmodule.exports = {\n    //other configurations\n    devServer: {\n        before: function (app, server) {\n            app.use(chainResponse([path.join(__dirname, \"modules\")], { debug: true }));\n        }\n    }\n};\n```\n\n### Express Web\n\n参考例子: [samples/express-sample](./samples/express-sample)\n\n```javascript\nconst { chainResponse } = require(\"express-middleware-chain-response\");\napp.use(chainResponse([path.join(__dirname, \"modules\")], { debug: true }));\n```\n\n## 模块定义\n\n[例子如下](./src/modules/module-sample.js):\n\n```javascript\n/**\n * 模块例子\n */\nconst chainModule = {\n    isOpen: false, //是否开启\n    priority: 5, //优先级, 默认100, 值越高越先执行\n    name: \"module name\", //模块名称(方便调试)\n\n    /**\n     * 模块和本次请求是否匹配\n     * @param {{method: string, originalUrl: string, path: string, query: string, xhr: boolean, getHeader: function(string): string request: Object}} param0 请求信息\n     * @returns {Promise | boolean}\n     */\n    isMatch({ method, originalUrl, path, query, xhr, getHeader, request }) {\n        /**\n         * method: HTTP method\n         * originalUrl: 请求的原始URL, 包含URL参数\n         * path: URL路径, 不包含URL参数\n         * query: URL查询参数\n         * xhr: 是否是Ajax请求(判断请求头 X-Requested-With 值是否是 XMLHttpRequest) \n         * getHeader: 获取请求头\n         * request: express 框架包裹的请求对象 http://www.expressjs.com.cn/4x/api.html#req\n         */\n\n        // 返回值必须是boolean类型\n        return false; \n\n        // 或者Promise对象\n        return Promise.resolve();\n    },\n    /**\n     *  获取响应内容\n     * @param {{method: string, originalUrl: string, path: string, query: string, xhr: boolean, request: Object}} param0 请求信息\n     * @param {Object} matchResult isMatch 返回的结果(fulfill 的值)\n     * @param {{content: string, headers: Array.\u003cstring, string\u003e}} prevResponse 上一个模块的 getResponse 返回返回值\n     * @param {{name: string, priotiy: number, isMatch: function():Promise, getResponse: function():Promise}} handledModules 已处理模块堆栈\n     * @returns {Promise\u003c{content: string, headers: Object.\u003cstring, string\u003e}\u003e} \n     */\n    getResponse({ method, originalUrl, path, query, xhr, request }, matchResult, prevResponse, handledModules) {\n        // 返回对象必须包含 content 和 headers \n        return {\n            content: \"\",\n            headers: {\n                // \"Content-Type\": \"application/json\"\n            }\n        };\n        // 也可以返回一个Promise\n        return Promise.resolve({\n            content: \"\",\n            headers: {\n                // \"Content-Type\": \"application/json\"\n            }\n        });\n    }\n};\n\n/**\n * 可以直接导出对象\n */\nmodule.exports = chainModule;\n\n/**\n * 也可以导出一个函数, 函数返回一个对象:\n * module.exports = () =\u003e chainModule;\n */\n```\n\n\n\n## 其他\n\n访问 `/@alanlib/express-middleware-chain-response` 可以开启/关闭此中间件的拦截, 通过`chainResponse`的参数`options.switchPath`可以改变路径.\n\n下一步支持 request.body 和图片等二进制文件mock.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falanwei43%2Fexpress-middleware-chain-response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falanwei43%2Fexpress-middleware-chain-response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falanwei43%2Fexpress-middleware-chain-response/lists"}