重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
在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=)]
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("监听3003端口");
1.3 运行服务现在我们创建node服务的代码已完成,只需要运行它啦。
在当前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=)
开启服务后,我们在浏览器输入: localhost:3003/index.html即可访问。
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧