1 Star 0 Fork 1

666/node.js-test

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server.js 3.96 KB
一键复制 编辑 原始数据 按行查看 历史
大胃王 提交于 2015-09-15 17:29 . first
var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
//配置
var config = {
port: 80,
denyAccess: ['./httpserver.js', './src/requirecache.js'],
localIPs: ['127.0.0.1'],
srcpath: '/src'
};
//开始HTTP服务器
http.createServer(processRequestRoute).listen(config.port);
console.log("Server has started. port:"+config.port);
//路由URL
function processRequestRoute(request, response) {
var pathname = url.parse(request.url).pathname;
if (pathname === '/') {
pathname = "/index.html"; //默认页面
}
var ext = path.extname(pathname);
var localPath = ''; //本地相对路径
var staticres = false; //是否是静态资源
if (ext.length > 0) {
localPath = '.' + pathname;
staticRes = true;
} else {
localPath = '.' + config.srcpath + pathname + '.js';
staticRes = false;
}
//禁止远程访问
if (config.denyAccess && config.denyAccess.length > 0) {
var islocal = false;
var remoteAddress = request.connection.remoteAddress;
for (var j = 0; j < config.localIPs.length; j++) {
if (remoteAddress === config.localIPs[j]) {
islocal = true;
break;
}
}
if (!islocal) {
for (var i = 0; i < config.denyAccess.length; i++) {
if (localPath === config.denyAccess[i]) {
response.writeHead(403, { 'Content-Type': 'text/plain' });
response.end('403:Deny access to this page');
return;
}
}
}
}
//禁止访问后端js
if (staticRes && localPath.indexOf(config.srcpath) >= 0) {
response.writeHead(403, { 'Content-Type': 'text/plain' });
response.end('403:Deny access to this page');
return;
}
fs.exists(localPath, function (exists) {
if (exists) {
if (staticRes) {
staticResHandler(localPath, ext, response); //静态资源
} else {
try {
var handler = require(localPath);
if (handler.processRequest && typeof handler.processRequest === 'function') {
handler.processRequest(request, response); //动态资源
} else {
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.end('404:Handle Not found');
}
} catch (exception) {
console.log('error::url:' + request.url + 'msg:' + exception);
response.writeHead(500, { "Content-Type": "text/plain" });
response.end("Server Error:" + exception);
}
}
} else { //资源不存在
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.end('404:File Not found');
}
});
}
//处理静态资源
function staticResHandler(localPath, ext, response) {
fs.readFile(localPath, "binary", function (error, file) {
if (error) {
response.writeHead(500, { "Content-Type": "text/plain" });
response.end("Server Error:" + error);
} else {
response.writeHead(200, { "Content-Type": getContentTypeByExt(ext) });
response.end(file, "binary");
}
});
}
//得到ContentType
function getContentTypeByExt(ext) {
ext = ext.toLowerCase();
if (ext === '.htm' || ext === '.html')
return 'text/html';
else if (ext === '.js')
return 'application/x-javascript';
else if (ext === '.css')
return 'text/css';
else if (ext === '.jpe' || ext === '.jpeg' || ext === '.jpg')
return 'image/jpeg';
else if (ext === '.png')
return 'image/png';
else if (ext === '.ico')
return 'image/x-icon';
else if (ext === '.zip')
return 'application/zip';
else if (ext === '.doc')
return 'application/msword';
else
return 'text/plain';
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/253498229/node.js-test.git
git@gitee.com:253498229/node.js-test.git
253498229
node.js-test
node.js-test
master

搜索帮助