2017-08-29 41 views
0

我在使用ExpressJ的node.js上使用socket.io。當我提供我的html頁面時,我在腳本標記中硬編碼了socket.io.js文件鏈接: <script src="/socket.io/socket.io.js"></script>客戶端上的Socket.io路徑,如何將其與我的node.js服務器目錄樹相匹配?

我不知道如何將它匹配到我的目錄樹。這是因爲如下:

enter image description here

它位於埋在在 '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"); 
}); 

回答

1

你只需要改變server.listenhttp.listen,也將努力

+0

太棒了,修復了它。 – erv

0

socket.io文件是由節點服務器,當你開始聽插座端口動態服務。它不存在於目錄結構中。

在你的情況下,你需要添加腳本路徑爲http://YOUR_NODE_SERVER_HOST_NAME:SOCKET_PORT/socket.io/socket.io.js。我的情況是http://localhost:3000/socket.io/socket.io.js

+0

這是越來越受我的代碼,擔任了在動態以上的路徑。所以當我硬編碼它時,我得到了相同的錯誤(有時它說404錯誤,其他時間,這只是一個紅色的十字錯誤與該網址和鏈接到它的HTML文件中的行。控制檯) – erv

相關問題