us = {}; // us.qs = require("querystring"); //引用文件解析包 // us.http = require("http"); //引用http server服务 // 引入Express模块 const express = require('express'); const path = require('path'); us.http = require("http"); //引用http server服务 // 创建Express应用 const app = express(); // 设置静态文件目录 app.use(express.static(path.join(__dirname, '/'))); us.server = us.http.createServer(function (req, res) { // 定义路由 // app.all('/', (req, res) => { //设置跨域 res.writeHead(200, { "Content-Type": "application/json", 'Access-Control-Allow-Origin': '*', "Connection": "keep-alive", 'Access-Control-Allow-Headers': 'Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With' }); var data = [{ "name": "John", "age": 30 }]; for (var i = 0; i < 100; i++) { data.push(data[0]); } setInterval(function () { res.write(JSON.stringify(data)); }, 2000); }); // 启动服务器 app.listen(3000, () => { console.log('Server is running on port 3000'); }); /** * 监听1473端口的处理 * **/ us.server.listen('1473', '', function () { //在控制台输出监听提示 console.log("开始监听" + us.server.address().port + "......"); });