当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 1 Fork 5

ColinUED/ssr
暂停

forked from 小弟调调/sgo 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.js 5.12 KB
一键复制 编辑 原始数据 按行查看 历史
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
net = require("net"),
color = require('colors-cli'),
iconv = require('iconv-lite'),
ctype = require("./lib/content-type"),
catalog = require('./lib/catalog'),
confproxy = require('./lib/get-proxy-config'),
query = require("querystring"), //解析POST请求
__port = 1987,
cors = false,
server;
require('colors-cli/toxic');
module.exports = server;
connListener = function(request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri),
_header = !cors ? {
}:{
"Access-Control-Allow-Origin":"*",
'Access-Control-Allow-Methods':'GET,PUT,POST,DELETE,OPTIONS',
'Access-Control-Allow-Headers':'Content-Type, Authorization, Content-Length, X-Requested-With, Accept, x-csrf-token, origin'
};
var ext = path.parse(request.url.replace(/\?.*$/g, "")).ext.replace('.','');
var pxval = confproxy[request.method+' '+uri];
if(pxval){
var postData = null,arr = [];
request.addListener("data",function(postchunk){
arr.push(postchunk)
})
//POST结束输出结果
request.addListener("end",function(){
var data= Buffer.concat(arr).toString(),ret;
try{
ret = JSON.parse(data);
}catch(err){}
request.body = ret;
if(typeof pxval === "function"){
pxval = pxval(ret?ret:data,request.url);
}
response.writeHead(200, {
"Content-Type":(function(){
if(isJson(pxval) || Object.prototype.toString.call(pxval)){
return ctype.getContentType('json') + ';charset=utf-8';
}else if(typeof pxval == 'string'){
return ctype.getContentType('html') + ';charset=utf-8';
}
return '';
})()
});
response.writeHead(200,_header);
response.write( iconv.encode(JSON.stringify(pxval), 'utf-8').toString('binary') , "binary");
response.end();
commandLog(200,request,response)
})
return;
}
if(ext) _header['Content-Type'] = ctype.getContentType(ext);
// url 解码
filename = decodeURIComponent(filename);
var html = catalog(process.cwd()+request.url);
if( fs.existsSync(filename) && fs.statSync(filename).isDirectory() && fs.existsSync(filename + '/index.html') ) filename += '/index.html';
if( fs.existsSync(filename) && fs.statSync(filename).isFile() ){
fs.readFile(filename, "binary", function(err, file) {
response.writeHead(200,_header);
response.write(file, "binary");
response.end();
commandLog(200,request,response)
return;
});
}else{
response.writeHead(404, {});
response.write(html);
response.end();
commandLog(404,request,response)
return;
}
}
function isJson(obj){
return typeof(obj) == "object"
&& Object.prototype.toString.call(obj).toLowerCase() == "[object object]"
&& !obj.length;
}
// 命令行颜色显示
function commandLog(staus,request,response){
var code = response.statusCode;
if(code === 200){
console.log( ('INFO ' + request.method + ' ' + code.toString() ).green_bt + ' ' + request.url )
}else{
console.log( ('INFO ' + request.method + ' ' + code.toString()).red_bt + ' ' + request.url );
}
}
// 检测port是否存在
function probe(port, callback) {
var server = net.createServer().listen(port)
var calledOnce = false
var timeoutRef = setTimeout(function () {
calledOnce = true
callback(false,port)
}, 2000)
timeoutRef.unref()
var connected = false
server.on('listening', function() {
clearTimeout(timeoutRef)
if (server)
server.close()
if (!calledOnce) {
calledOnce = true
callback(true,port)
}
})
server.on('error', function(err) {
clearTimeout(timeoutRef)
var result = true
if (err.code === 'EADDRINUSE')
result = false
if (!calledOnce) {
calledOnce = true
callback(result,port)
}
})
}
// 启动服务
function serverStart(_port){
probe(_port,function(bl,_pt){
if(bl === true){
// ssr(_pt)
server = http.createServer(connListener);
server = server.listen(parseInt(_pt, 10));
console.log("\n Static file server running at" + color.green("\n\n=> http://localhost:" + _pt ) + '\n');
}else{
serverStart(_pt+1)
}
})
}
function server(argv){
var pt = '';
if(argv && argv.port) pt = argv.port;
else pt = __port;
if(argv && argv.port === true) pt = __port;
(argv && argv.cors) ? cors = true : cors = false;
if(argv && argv.proxy){
confproxy = confproxy(argv.proxy);
}
serverStart(pt);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
NodeJS
1
https://gitee.com/SOHONET/ssr.git
[email protected]:SOHONET/ssr.git
SOHONET
ssr
ssr
master

搜索帮助