项目作者: alanwei43

项目描述 :
express middleware for chain response
高级语言: JavaScript
项目地址: git://github.com/alanwei43/express-middleware-chain-response.git
创建时间: 2019-09-21T02:50:10Z
项目社区:https://github.com/alanwei43/express-middleware-chain-response

开源协议:MIT License

下载


Express 中间件

主要用于 webpack-dev-server mock接口使用.

安装

  1. npm install @alanlib/express-middleware-chain-response

使用方式

函数定义

  1. /**
  2. * 获取 express 中间件
  3. * @param {Array.<string | {isOpen: boolean, isMatch: function(): Promise<boolean> getResponse: function(): Promise}>} modules 模块(可以是已经加载好的模块数组, 也可以是模块文件所在目录, 或者模块的文件路径)
  4. * @param {{debug: boolean, switchPath: string, currentSwitchOn: boolean}} options 选项
  5. * @returns {function} express中间件
  6. */
  7. function chainResponse(modules, options){
  8. //...
  9. }
  • options:
    • debug: boolean 开启会输出更多log日志
    • switchPath: string 用来开启/关闭此中间件的拦截功能, 默认 /@alanlib/express-middleware-chain-response
    • currentSwitchOn: boolean 默认开启/关闭拦截功能

可以指定模块所在目录: chainResponse(["../modules"]), 或模块文件: chainResponse(["../modules/jsonp-module.js"])

也可以直接传入模块: chainResponse([require("./module1"), require("./module2")])

或者混合使用: chainResponse(["../modules-dir", "./module1.js", require("./module1"), require("./module2")])

webpack-dev-server

参考例子: samples/webpack-dev-sever-sample

  1. const { chainResponse } = require("express-middleware-chain-response");
  2. module.exports = {
  3. //other configurations
  4. devServer: {
  5. before: function (app, server) {
  6. app.use(chainResponse([path.join(__dirname, "modules")], { debug: true }));
  7. }
  8. }
  9. };

Express Web

参考例子: samples/express-sample

  1. const { chainResponse } = require("express-middleware-chain-response");
  2. app.use(chainResponse([path.join(__dirname, "modules")], { debug: true }));

模块定义

例子如下:

  1. /**
  2. * 模块例子
  3. */
  4. const chainModule = {
  5. isOpen: false, //是否开启
  6. priority: 5, //优先级, 默认100, 值越高越先执行
  7. name: "module name", //模块名称(方便调试)
  8. /**
  9. * 模块和本次请求是否匹配
  10. * @param {{method: string, originalUrl: string, path: string, query: string, xhr: boolean, getHeader: function(string): string request: Object}} param0 请求信息
  11. * @returns {Promise | boolean}
  12. */
  13. isMatch({ method, originalUrl, path, query, xhr, getHeader, request }) {
  14. /**
  15. * method: HTTP method
  16. * originalUrl: 请求的原始URL, 包含URL参数
  17. * path: URL路径, 不包含URL参数
  18. * query: URL查询参数
  19. * xhr: 是否是Ajax请求(判断请求头 X-Requested-With 值是否是 XMLHttpRequest)
  20. * getHeader: 获取请求头
  21. * request: express 框架包裹的请求对象 http://www.expressjs.com.cn/4x/api.html#req
  22. */
  23. // 返回值必须是boolean类型
  24. return false;
  25. // 或者Promise对象
  26. return Promise.resolve();
  27. },
  28. /**
  29. * 获取响应内容
  30. * @param {{method: string, originalUrl: string, path: string, query: string, xhr: boolean, request: Object}} param0 请求信息
  31. * @param {Object} matchResult isMatch 返回的结果(fulfill 的值)
  32. * @param {{content: string, headers: Array.<string, string>}} prevResponse 上一个模块的 getResponse 返回返回值
  33. * @param {{name: string, priotiy: number, isMatch: function():Promise, getResponse: function():Promise}} handledModules 已处理模块堆栈
  34. * @returns {Promise<{content: string, headers: Object.<string, string>}>}
  35. */
  36. getResponse({ method, originalUrl, path, query, xhr, request }, matchResult, prevResponse, handledModules) {
  37. // 返回对象必须包含 content 和 headers
  38. return {
  39. content: "",
  40. headers: {
  41. // "Content-Type": "application/json"
  42. }
  43. };
  44. // 也可以返回一个Promise
  45. return Promise.resolve({
  46. content: "",
  47. headers: {
  48. // "Content-Type": "application/json"
  49. }
  50. });
  51. }
  52. };
  53. /**
  54. * 可以直接导出对象
  55. */
  56. module.exports = chainModule;
  57. /**
  58. * 也可以导出一个函数, 函数返回一个对象:
  59. * module.exports = () => chainModule;
  60. */

其他

访问 /@alanlib/express-middleware-chain-response 可以开启/关闭此中间件的拦截, 通过chainResponse的参数options.switchPath可以改变路径.

下一步支持 request.body 和图片等二进制文件mock.