代码拉取完成,页面将自动刷新
var PORT = 3000;//
var wwwRootPath = "wwwroot";
var http = require('http');
var url = require('url');
var fs = require('fs');
var mine = require('./mine').types;//
var path = require('path');
var server = http.createServer(function (request, response) {
var pathname = url.parse(request.url).pathname;
if (pathname === "/") {
pathname = "index.html"
}
var realPath = path.join(wwwRootPath, pathname); //这里设置自己的文件名称;
console.log("访问文件",realPath);
var ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
fs.exists(realPath, function (exists) {
if (!exists) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write("This request URL " + pathname + " was not found on this server.");
response.end();
} else {
fs.readFile(realPath, "binary", function (err, file) {
if (err) {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.end(err);
} else {
var contentType = mine[ext] || "text/plain";
response.writeHead(200, {
'Content-Type': contentType
});
response.write(file, "binary");
response.end();
}
});
}
});
});
server.listen(PORT);
console.log("server ok");
console.log("请将需要的文件放入文件根目录:" + wwwRootPath);
console.log("访问的地址:http://localhost:"+PORT);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。