index.js 627 B

123456789101112131415161718192021222324
  1. const utils = require("./utils");
  2. function mocker(middlewares, { app }) {
  3. app.use(async function middleware(req, res, next) {
  4. const mockConfig = utils.requireUncached("./mock.config");
  5. if (mockConfig.enable) {
  6. const path = req.path.split("?")[0];
  7. if (mockConfig.api[path]) {
  8. console.log("Mocker enter");
  9. console.log("mock:", req.path);
  10. const data = await mockConfig.api[path](req);
  11. data._debug = {
  12. msg: "这是一个mock数据",
  13. };
  14. res.json(data);
  15. return;
  16. }
  17. }
  18. next();
  19. });
  20. return middlewares;
  21. }
  22. module.exports = mocker;