Javascript 客户端上的Socket.io路径,如何将其与我的node.js服务器目录树匹配?

Javascript 客户端上的Socket.io路径,如何将其与我的node.js服务器目录树匹配?,javascript,node.js,express,path,socket.io,Javascript,Node.js,Express,Path,Socket.io,我在node.js上使用socket.io和Expressjs。提供html页面时,我将socket.io.js文件链接硬编码在脚本标记中: 我不确定如何将其与我的目录树匹配。详情如下: 它位于“node_modules”文件夹中 我的index.js如下所示: const PORT = 3000; const express = require("express"); const server = express(); const http = require("http").Server

我在node.js上使用socket.io和Expressjs。提供html页面时,我将socket.io.js文件链接硬编码在脚本标记中:

我不确定如何将其与我的目录树匹配。详情如下:

它位于“node_modules”文件夹中

我的index.js如下所示:

const PORT = 3000;
const express = require("express");
const server = express();
const http = require("http").Server(server);
const path = require("path");
const io = require("socket.io")(http);

server.use(express.static(path.join(__dirname + "/public")));
server.use(express.static(__dirname + "/public/css"));
server.use(express.static(__dirname + "/public/js"));


server.listen(PORT, function() {
      console.log("server listening on port " + PORT);
});

 io.on("connection", function(socket){
      console.log("user connected");
});

您只需将
服务器。listen
更改为
http。listen
即可工作

太棒了,修复了它。