mock.ts 801 B

1234567891011121314151617181920212223242526272829303132333435
  1. import express from "express";
  2. import path from "path";
  3. import bodyParser from 'body-parser'
  4. import * as fs from "fs";
  5. const staticDir = path.resolve(__dirname, "test");
  6. console.log(staticDir);
  7. let startup = false;
  8. export async function createServer(port: number) {
  9. if (startup) {
  10. return;
  11. }
  12. const app = express();
  13. app.use(express.static(staticDir));
  14. app.use(bodyParser())
  15. app.listen(port);
  16. startup = true;
  17. app.use((req, res, next) => {
  18. const paths = req.url.split("/")
  19. const scene = paths[1]
  20. const filename = paths[paths.length - 1]
  21. const p = path.resolve(staticDir, scene, "./attach", filename)
  22. console.log(p, req.body)
  23. fs.writeFileSync(p, JSON.stringify(req.body))
  24. res.json({code: 0, msg: 'ok'})
  25. })
  26. console.log("模拟环境已开启");
  27. }