在node官網(wǎng) https://nodejs.org/en/ 下載最新的node并逐步安裝。
打開cmd,輸入 node -v這時(shí)成功的話會出現(xiàn)node的版本號。如下圖:
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-OszqdLok-1669876811332)(https://cdn.nlark.com/yuque/0/2022/png/26124887/1648718162240-9db94a4a-28ab-4130-b633-25aed151e06b.png#clientId=ufa998507-66b8-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=ud2ea6fcb&margin=%5Bobject%20Object%5D&name=%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20220331171546.png&originHeight=269&originWidth=400&originalType=binary&ratio=1&rotation=0&showTitle=false&size=7086&status=done&style=none&taskId=u11367da5-5317-4413-b0f6-15b2742d3cc&title=)]
var url = require("url"),
fs = require("fs"),
http = require("http"),
path = require("path");
http.createServer(function (req, res) {
var pathname = __dirname + url.parse("/dist"+req.url).pathname;//資源指向dist目錄
if (path.extname(pathname) == "") {
pathname += "/";
}
if (pathname.charAt(pathname.length - 1) == "/") {
pathname += "index.html";
}
fs.exists(pathname, function (exists) {
if (exists) {
switch(path.extname(pathname)){
case ".html":
res.writeHead(200, {"Content-Type": "text/html"});
break;
case ".js":
res.writeHead(200, {"Content-Type": "text/javascript"});
break;
case ".css":
res.writeHead(200, {"Content-Type": "text/css"});
break;
case ".gif":
res.writeHead(200, {"Content-Type": "image/gif"});
break;
case ".jpg":
res.writeHead(200, {"Content-Type": "image/jpeg"});
break;
case ".png":
res.writeHead(200, {"Content-Type": "image/png"});
break;
default:
res.writeHead(200, {"Content-Type": "application/octet-stream"});
}
fs.readFile(pathname, function (err, data) {
res.end(data);
});
} else {
res.writeHead(404, {
"Content-Type": "text/html"
});
res.end("404 Not Found");
}
});
}).listen(3003);
console.log("監(jiān)聽3003端口");
1.3 運(yùn)行服務(wù)現(xiàn)在我們創(chuàng)建node服務(wù)的代碼已完成,只需要運(yùn)行它啦。
在當(dāng)前server.js目錄下,打開cmd或者powershell,輸入node server.js

開啟服務(wù)后,我們在瀏覽器輸入: localhost:3003/index.html即可訪問。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
文章題目:搭建node服務(wù)器訪問本地文件-創(chuàng)新互聯(lián)
URL分享:http://jinyejixie.com/article40/eipeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、面包屑導(dǎo)航、服務(wù)器托管、域名注冊、電子商務(wù)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容