nodejs,socket.io和expressjs不能连接

我有一个文件夹结构,如:

socket - project - main.js -node_modules - node_modules_folder_here - text.html 

我的main.js文件如下所示:

 var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res){ console.log("inside") res.send('<h1>Hello again</h1>'); }); io.on('connection', function(socket){ console.log("socket") console.log('a user connected'); }); http.listen(3000, function(){ console.log('listening on *:3000'); }); 

我使用node main.js运行服务器,一切工作正常我的test.html如下所示:

 <body> <h3>Hellooww world</h3> <script src="/socket.io/socket.io.js"></script> <script type="text/javascript"> var socket = io(); console.log("check io", socket) </script> </body> </html> 

我的package.json像:

 "author": "", "license": "ISC", "dependencies": { "babel-core": "^6.24.0", "babel-loader": "^6.4.0", "express": "^4.15.2", "node-fetch": "^1.6.3", "promise": "^7.1.1", "socket.io": "^1.7.3", "webpack": "^2.2.1", "xmlhttprequest": "^1.8.0" 

我只是按照socket.io教程,并得到这个错误

 GET file:///socket.io/socket.io.js net::ERR_FILE_NOT_FOUND test.html:10 Uncaught ReferenceError: io is not defined 

另外,如果我使用这个套接字服务器为另一个项目如何调用它?

这里有什么问题? 我只是按照socket.io教程,我在这里呢? 什么是错的,为什么?

您的浏览器正在寻找file:///socket.io/socket.io.js ,它假定file://协议使我认为你直接打开你的HTML文件。 您在srcstring之后也有一个缺less的报价。

确保你正在加载localhost:3000