test.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. us = {};
  2. // us.qs = require("querystring"); //引用文件解析包
  3. // us.http = require("http"); //引用http server服务
  4. // 引入Express模块
  5. const express = require('express');
  6. const path = require('path');
  7. // 创建Express应用
  8. const app = express();
  9. // 设置静态文件目录
  10. app.use(express.static(path.join(__dirname, '/')));
  11. // 定义路由
  12. app.all('/', (req, res) => {
  13. //设置跨域
  14. res.writeHead(200, {
  15. "Content-Type": "application/json",
  16. 'Access-Control-Allow-Origin': '*',
  17. "Connection": "keep-alive",
  18. 'Access-Control-Allow-Headers': 'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With'
  19. });
  20. var data = [{ "name": "John", "age": 30 }];
  21. for (var i = 0; i < 100; i++) {
  22. data.push(data[0]);
  23. }
  24. setInterval(function () {
  25. res.write(JSON.stringify(data));
  26. }, 2000);
  27. });
  28. // 启动服务器
  29. app.listen(3000, () => {
  30. console.log('Server is running on port 3000');
  31. });