国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

node.js - node項目找不到文件,index.js輸出index.html

瀏覽:169日期:2023-10-10 14:11:03

問題描述

node.js - node項目找不到文件,index.js輸出index.html

var app = require(’express’)();var http = require(’http’).Server(app);var io = require(’socket.io’)(http);app.get(’/’, function(req, res){ res.sendFile(__dirname + ’/index.html’);});io.on(’connection’, function(socket){ console.log(’a user connected’);});http.listen(80, function() { console.log(’listening on *:80’);});

這是index.js源碼,然后訪問localhost

node.js - node項目找不到文件,index.js輸出index.htmljquery找不到,socket.io.js倒是找到了。。。。這是index.html引入部分

node.js - node項目找不到文件,index.js輸出index.html

問題解答

回答1:

我理解你的疑問有兩個問題

為什么能找到/socket.io/socket.io.js, 這是因為如果Socket.io服務監聽在你的http服務上,它會自動提供http://localhost:<port>/socket.io/socket.io.js這條路由(其實是攔截了所有/socket.io開頭的請求, 并且請求的socket.io.js會被解析到socket.io-client/socket.io.js,所以你可以看看你前端獲取到的js,其實是socket.io-client模塊里面的文件,并不是socket.io模塊里面的)。你不需要復制到對外的靜態文件目錄,或者手工提供該服務。

為什么你自己的/js/jquery-2.0.3.min.js卻找不到,因為這個靜態文件服務,沒有模塊給你提供,所以你需要自己手動提供,在你上面的index.js代碼稍微改造一下,結果如下:

//改動一下, 提出expressvar express = require(’express’)var app = express();var http = require(’http’).Server(app);var io = require(’socket.io’)(http);//提供靜態文件服務,這樣就能找到你的`jquery-2.0.3.min.js`文件app.use(express.static(__dirname));app.get(’/’, function(req, res){ res.sendFile(__dirname + ’/index.html’);});io.on(’connection’, function(socket){ console.log(’a user connected’);});// 最好不要直接監聽在80端口,改成8888http.listen(8888, function() { console.log(’listening on *:8888’);});

備注:最好不要直接監聽在80端口,你可以監聽在其他端口,然后掛一個nginx做為反向代理,這樣的處理方式可能更加緩和一點。 直接監聽在80端口太暴力了!個人意見,希望對你有幫助。

標簽: HTML
主站蜘蛛池模板: 稷山县| 特克斯县| 蓬溪县| 大田县| 达拉特旗| 依兰县| 鄱阳县| 古蔺县| 岢岚县| 济阳县| 长治县| 荥阳市| 民乐县| 长岭县| 唐海县| 邢台县| 翁牛特旗| 扶沟县| 淮安市| 富锦市| 定州市| 内黄县| 屯门区| 元朗区| 枣强县| 丽江市| 闽清县| 万源市| 安新县| 台东县| 敦化市| 百色市| 昂仁县| 西宁市| 淳化县| 城固县| 肥东县| 克东县| 上蔡县| 阿荣旗| 宜州市|