在相同的起源策略中,
localhost:8090并且
localhost不是相同的起源(不同的端口),因此
localhost/index.html无法连接到localhost:8090上的socket.io。
一种选择是启用index.html
localhost:8090(请参见下面的代码)。然后,您只需将“
index.html”放入服务器脚本的同一目录中,启动服务器并输入
localhost:8090浏览器。
var fs = require('fs');server = http.createServer(function(req, res){ fs.readFile(__dirname + '/index.html', 'utf-8', function(err, data) { // read index.html in local file system if (err) { res.writeHead(404, {'Content-Type': 'text/html'}); res.end('<h1>Page Not Found</h1>'); } else { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(data); } });});


