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

您的位置:首頁技術(shù)文章
文章詳情頁

node.js - node項(xiàng)目找不到文件,index.js輸出index.html

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

問題描述

node.js - node項(xiàng)目找不到文件,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項(xiàng)目找不到文件,index.js輸出index.htmljquery找不到,socket.io.js倒是找到了。。。。這是index.html引入部分

node.js - node項(xiàng)目找不到文件,index.js輸出index.html

問題解答

回答1:

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

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

為什么你自己的/js/jquery-2.0.3.min.js卻找不到,因?yàn)檫@個靜態(tài)文件服務(wù),沒有模塊給你提供,所以你需要自己手動提供,在你上面的index.js代碼稍微改造一下,結(jié)果如下:

//改動一下, 提出expressvar express = require(’express’)var app = express();var http = require(’http’).Server(app);var io = require(’socket.io’)(http);//提供靜態(tài)文件服務(wù),這樣就能找到你的`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’);});// 最好不要直接監(jiān)聽在80端口,改成8888http.listen(8888, function() { console.log(’listening on *:8888’);});

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

標(biāo)簽: HTML
主站蜘蛛池模板: 楚雄市| 万盛区| 清流县| 峨眉山市| 阳江市| 屏南县| 襄城县| 新平| 湛江市| 汉源县| 贡嘎县| 仲巴县| 越西县| 西贡区| 仪陇县| 九龙坡区| 石狮市| 德阳市| 冀州市| 马尔康县| 阿瓦提县| 湛江市| 喀喇| 五台县| 紫金县| 丹凤县| 阿坝| 唐山市| 丹东市| 阳谷县| 新龙县| 安陆市| 临湘市| 梨树县| 乌拉特后旗| 昭觉县| 金乡县| 东宁县| 曲周县| 尉氏县| 宜兰县|