搭建node服務(wù)器訪問本地文件-創(chuàng)新互聯(lián)

1.搭建node服務(wù)

成都創(chuàng)新互聯(lián)公司10多年企業(yè)網(wǎng)站制作服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計(jì)及高端網(wǎng)站定制服務(wù),企業(yè)網(wǎng)站制作及推廣,對(duì)成都發(fā)電機(jī)回收等多個(gè)行業(yè)擁有豐富的網(wǎng)站制作經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。1.1 安裝node環(huán)境

在node官網(wǎng) https://nodejs.org/en/ 下載最新的node并逐步安裝。

  • 來驗(yàn)證一下我們的環(huán)境

打開cmd,輸入 node -v這時(shí)成功的話會(huì)出現(xiàn)node的版本號(hào)。如下圖:
[外鏈圖片轉(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=)]

1.2 搭建node服務(wù)
  • 首先新建一個(gè)server.js,內(nèi)容如下:
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
![微信圖片_20220331171848.png](/file/tupian/20221205/964c35471cc38c0563c0ad9c333c3866.jpg#clientId=ufa998507-66b8-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=u50375870&margin=[object Object]&name=微信圖片_20220331171848.png&originHeight=45&originWidth=458&originalType=binary&ratio=1&rotation=0&showTitle=false&size=1420&status=done&style=none&taskId=ud61caec1-b124-49cf-a77c-83e8ff223c3&title=)
開啟服務(wù)后,我們?cè)跒g覽器輸入: localhost:3003/index.html即可訪問。

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

文章題目:搭建node服務(wù)器訪問本地文件-創(chuàng)新互聯(lián)
URL分享:http://muchs.cn/article40/eipeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、面包屑導(dǎo)航、服務(wù)器托管、域名注冊(cè)、電子商務(wù)、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)