webio-node/route/ResTool.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-03-22 05:35:45 +00:00
const errList = require('../includes/errorManager');
function send(req, res) {
if ('db' in res && typeof res.db == 'object' && 'close' in res.db && typeof res.db.close == 'function') res.db.close();
if ('db' in res && typeof res.db == 'object' && 'release' in res.db && typeof res.db.release == 'function') res.db.release();
2017-03-22 05:35:45 +00:00
let lngs = req.headers['accept-language'] || '';
lngs = lngs.split(',');
2017-03-22 05:35:45 +00:00
let lng = null;
if (lngs.length > 0) {
lng = lngs[0].substring(0, 2);
}
let json = {};
if (!('api_res' in res)) {
json = {
errorCode: 'ERR9999',
message: 'api not found',
status: 0
};
} else if (typeof res.api_res != 'object') {
json = {
errorCode: res.api_res,
message: errList(res.api_res, lng),
status: 0
};
} else {
json = {
data: res.api_res,
status: 1
};
}
2017-03-31 09:14:42 +00:00
if('sys_err' in res) {
json.sys_err = res.sys_err
}
2017-03-22 05:35:45 +00:00
res.send(json);
}
2017-03-31 09:14:42 +00:00
const err = (res, err, n, code = null) => {
if(process.env.NODE_ENV != 'production') res.sys_err = 'toString' in err && typeof err.toString == 'function' ? err.toString() : err;
if(!code) return n();
return n(code);
}
module.exports = { send, err };