我正在管理從'/ js /'開始的一種客戶端請求,這意味着服務器端的根文件夾或可能位於'/ public/js /'以供進一步使用。 好了,問題是,我得到這個消息:NodeJs響應客戶端的url請求到另一個
//server side
GET /js/socket.io/socket.io.js 304 7ms
// client side
Uncaught ReferenceError: require is not defined
Uncaught ReferenceError: io is not defined
[X]來源
// index.html
<script src="/js/socket.io/socket.io.js"></script>
// app.js
app.get('/js/*', function (req, res) {
res.sendfile(__dirname + req.url.substr('/js'.length));
});
但是,當我更改爲固定的路徑它的工作。
[O]來源
// index.html
<script src="/socket.io/socket.io.js"></script>
// app.js
app.get('/socket.io/socket.io.js', function (req, res) {
res.sendfile(__dirname + '/socket.io/socket.io.js');
});
這是一種奇怪的,不是嗎?任何人都可以找出錯誤的位置。
非常感謝。
出現在客戶端上的錯誤是以下幾點: 未捕獲的ReferenceError:要求沒有定義 未捕獲的ReferenceError:IO沒有定義 但成功案例不存在問題。 – Kevin 2013-02-19 11:04:42
聽起來好像你正在嘗試在客戶端中使用服務器版本的'socket.io.js'。 'socket.io'爲'/ socket.io/socket.io.js'安裝它自己的處理程序,所以你不必爲你的服務器端代碼創建一個處理程序(參見http://socket.io/#如何使用) – robertklep 2013-02-19 11:12:37
謝謝,我已經刪除了這個處理程序,並工作。但我仍然很好奇第一個案例實際上是發送位於服務器端的/socket.io/socket.io.js中的文件。這意味着它與第二種情況一樣,不是嗎? – Kevin 2013-02-20 03:48:37