node -v npm -v
npm config set registry https://registry.npm.taobao.org
npm config set registry https://registry.npmjs.org
var server = require('./server.js');
var rootpath = 'root';
var sv = server.create({
port: '9587',
host: '127.0.0.1',
root: rootpath
});
•mime.js
var types = {
"css": "text/css",
"less": "text/css",
"gif": "image/gif",
"html": "text/html",
"ejs": "text/html",
"ico": "image/x-icon",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"pdf": "application/pdf",
"png": "image/png",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tiff": "image/tiff",
"txt": "text/plain",
"wav": "audio/x-wav",
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"xml": "text/xml",
"default": "text/plain"
};
module.exports = function (ext) {
return types[ext] || 'text/plain'
}
var http = require('http');
var path = require('path');
var fs = require('fs');
var url = require("url");
var mime = require('./mime.js');
function getPromise(cbk) {
return (new Promise(cbk));
}
exports.create = function (opts) {
var root = opts.root;
var sv = http.createServer();
function request(request, response) {
var pathname = decodeURIComponent(url.parse(request.url).pathname);
var realPath = path.resolve(path.join(root, pathname));//请求的实际路径
getPromise(function (resolve, reject) {
fs.exists(realPath, function (isExists) {//判断路径是否存在
isExists ? resolve() : reject();
});
}).catch(function () {
resWrite(response, '404', 'html', '<h1>404</h1>file or dir : <h3>' + pathname + '</h3>not found');
}).then(function () {
return getPromise(function (resolve, reject) {
fs.stat(realPath, function (err, stat) {//判断路径是文件还是文件夹
if (err) {
reject(err);
} else {
resolve(stat);
}
})
}).then(function (stat) {
if (stat.isFile()) {//路径对应的是一个文件
resFile(response, realPath);
} else if (stat.isDirectory()) {//路径对应的是一个文件夹
var defaultIndexPath = path.resolve(realPath, 'index.html');
return getPromise(function (resolve, reject) {
fs.exists(defaultIndexPath, function (isExists) {
if (isExists) {//如果该文件夹内有index.html
resolve(true);
} else {//该文件夹内没有index.html 则 显示该文件夹的内容列表
resolve(false);
}
})
}).then(function (isExistsIndex) {
if (isExistsIndex) {
resFile(response, defaultIndexPath);
} else {
return getPromise(function (resolve, reject) {
fs.readdir(realPath, function (err, list) {
if (err) {
reject(err);
} else {
resolve(list);
}
})
}).then(function (list) {
var pmlist = list.map(function (item) {
return (new Promise(function (resolve, reject) {
fs.stat(path.resolve(realPath, item), function (err, stat) {
if (err) {
console.error(err);
resolve('');
} else if (stat.isFile()) {
resolve(`<li class="file"><a href="${item}">${item}</a></li>`);
} else if (stat.isDirectory()) {
resolve(`<li class="dir"><a href="${item}/">${item}</a></li>`);
} else {
resolve('');
}
})
}));
});
Promise.all(pmlist).then(function (linkList) {
var links = '<ul>';
links += '<li class="dir"><a href="../">../</a></li>';
links += linkList.join('');
links += '</ul>';
var dirPage = `
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<style>
a{color:blue;text-decoration: none;}
.dir a{color:orange}
</style>
</head>
<body>
${links}
</body>
</html>
`;
resWrite(response, '200', 'html', dirPage);
});
}).catch(function (err) {
resWrite(response, '500', 'default', err.toString());
})
}
})
} else {//既不是文件也不是文件夹
resWrite(response, '404', 'html', '<h1>404</h1>file or dir : <h3>' + pathname + '</h3>not found');
}
}).catch(function (err) {
resWrite(response, '500', 'default', err.toString());
})
})
}
sv.on('request', request);
sv.listen(opts.port, opts.host);
return sv;
};
function resFile(response, realPath) {//输出一个文件
fs.readFile(realPath, function (err, data) {
if (err) {
resWrite(response, '500', 'default', err.toString());
} else {
var ext = path.extname(realPath).toLocaleLowerCase();
ext = (ext ? ext.slice(1) : 'unknown');
resWrite(response, '200', ext, data);
}
});
}
function resWrite(response, statusCode, mimeKey, data) {
response.writeHead(statusCode, {'Content-Type': mime(mimeKey)});
response.end(data);
}
node index.js
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有