1 Star 2 Fork 0

richard / egg-example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.js 2.13 KB
一键复制 编辑 原始数据 按行查看 历史
richard 提交于 2018-08-17 16:58 . 1.mongodb 数据库集成
module.exports = app => {
//内置缓存
app.cache = {
lastCursor: '',//抓取api page页
errorNum: 0, //错误数量
mysqlState: false //默认关闭mysql
};
app.beforeStart(async () => {
//设置自定义 服务、参数等 ,例如 注入缓存readis 等等
app.logger.info("app beforeStart... begin")
// check 网络通信
// 保证应用启动监听端口前数据已经准备好了
// 后续数据的更新由定时任务自动触发
app.logger.info("app runSchedule... begin")
// await app.runSchedule('update_cache');
app.logger.info("app runSchedule... ok !")
//检查 数据库 服务是否连接成功
app.checkMySqlService = function () {
var net = require('net');
var server = net.createServer(function (connection) { });
server.listen(3306, function () {
app.logger.info('3306 prot server is listening no mysql !');
app.logger.info("app database connecttion error!");
server.close();
});
server.on('error', function (err) {
app.logger.info('3306 prot server is error begin connecttion mysql ...');
app.database = app.mysql.createInstance({
// host
host: '127.0.0.1',
// 端口号
port: '3306',
// 用户名
user: 'test',
// 密码
password: 'test',
// 数据库名
database: 'news',
});
//mysql 开启成功
app.cache.mysqlState = true;
app.logger.info("app mysql database connecttion ok!")
})
};
// app.checkMySqlService();
});
app.once('server', server => {
// websocket
});
app.on('error', (err, ctx) => {
// report error
});
app.on('request', ctx => {
// log receive request
ctx.logger.info("request starttime...", ctx.starttime);
ctx.logger.info("request querystring ===", ctx.querystring);
});
app.on('response', ctx => {
// ctx.starttime is set by framework
const used = Date.now() - ctx.starttime;
// log total cost
ctx.logger.info("response endtime...", Date.now());
ctx.logger.info("response time...", used);
});
};
JavaScript
1
https://gitee.com/richard1015/egg-example.git
git@gitee.com:richard1015/egg-example.git
richard1015
egg-example
egg-example
master

搜索帮助