搭建node服务器访问本地文件

1.搭建node服务

1.1 安装node环境

在node官网 https://nodejs.org/en/ 下载最新的node并逐步安装。

  • 来验证一下我们的环境

打开cmd,输入 node -v这时成功的话会出现node的版本号。如下图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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服务
  • 首先新建一个server.js,内容如下:
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("<h1>404 Not Found</h1>");
        }
    });
}).listen(3003);
console.log("监听3003端口");

1.3 运行服务

现在我们创建node服务的代码已完成,只需要运行它啦。
在当前server.js目录下,打开cmd或者powershell,输入node server.js
![微信图片_20220331171848.png](https://img-blog.csdnimg.cn/img_convert/964c35471cc38c0563c0ad9c333c3866.png#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=)
开启服务后,我们在浏览器输入: localhost:3003/index.html即可访问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值