UNPKG

gaiaminterface

Version:

103 lines (97 loc) 4.04 kB
var host = '42.96.173.180';//服务器监听的ip地址 var port = 8088; //服务器监听的端口号 var http = require('http'); var domain = require('domain'); //解析基本的文件夹未知 var qs = require('qs'); var path = require('path'); var basedir = path.resolve(__dirname, ".."); var source = path.resolve(__dirname); //读接口信息 var routes = require(path.join(source, 'route.json')); //读取配置信息 var conf = require(path.join(source, 'conf.json')); //读取Error信息 var ErrorInfoMation = require('./ErrorInformation.json'); //添加mysql数据连接 var mysql = require('mysql'); module.exports.getConnection = function () { if ((module.exports.connection) && (module.exports.connection._socket) && (module.exports.connection._socket.readable) && (module.exports.connection._socket.writable)) { return module.exports.connection; } console.log(((module.exports.connection) ? "UNHEALTHY SQL CONNECTION; RE" : "") + "连接到SQL."); var connection = mysql.createConnection({ host: conf.mysqlHost, user: conf.mysqlUser, database: conf.mysqlDatabase, password: conf.mysqlPassword, port: conf.mysqlPort, //中文读取 charset: "utf8" }); connection.connect(function (err) { if (err) { var d = domain.create(); d.on('error', function (e) { res.writeHead(105, {'Content-Type': 'text/plain', 'Content-Length': Buffer.byteLength(JSON.stringify(ErrorInfoMation.SystemError), 'utf8')}); res.end(JSON.stringify(ErrorInfoMation.SystemError)); console.log("SQL连接错误:" + err); }); var e; d.run(function () { e = new events.EventEmitter(); }); e.on('msg', function () { throw new Error("Exception:"); }); e.emit('error', new Error("error msg")); e.emit('msg'); console.log("SQL连接错误:" + err); } else { console.log("SQL连接成功"); } }); connection.on("close", function (err) { console.log("SQL连接关闭."); }); connection.on("error", function (err) { console.log("SQL连接错误:" + err); }); module.exports.connection = connection; return module.exports.connection; }; // 在应用程序启动时自动打开一个连接 module.exports.getConnection(); //创建服务器 http.createServer(function (req, res) { req.finish = false; for (var route in routes) { if (routes[route].path == req.url && routes[route].method == req.method) { var processor = require(path.join(source, "processors", req.url)); console.log(req.url); //设置30秒超时返回值 setTimeout(function () { if (!req.finish) { req.finish = true; res.writeHead(500, {'Content-Type': 'text/plain', 'Content-Length': Buffer.byteLength(JSON.stringify(ErrorInfoMation.RequestTimeOut), 'utf8')}); res.end(JSON.stringify(ErrorInfoMation.RequestTimeOut)); } }, 30000); req.on('data', function (data) { console.log('Server接收到的Data:' + data); }); processor.process(req, res, module.exports.getConnection()); process.on('uncaughtException', function (err) { console.log('捕获异常:' + err); }); return; } } //不合法的请求直接返回 res.writeHead(400, {'Content-Type': 'text/plain', 'Content-Length': Buffer.byteLength(JSON.stringify(ErrorInfoMation.NoValidURL), 'utf8')}); res.end(JSON.stringify(ErrorInfoMation.NoValidURL)); return; }).listen(port, host); console.log("服务器运行于:" + host + ":" + port);