Skip to content

Commit 17b6ff6

Browse files
FreaxFreax
authored andcommitted
示例
1 parent 1612d20 commit 17b6ff6

File tree

1 file changed

+69
-57
lines changed

1 file changed

+69
-57
lines changed

app.js

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,81 @@
11
const Koa = require('koa');
2-
// Koa application is now a class and requires the new operator.
32
const app = new Koa();
4-
5-
/**
6-
* Created by huangyanxiong on 16-12-1.
7-
*/
8-
//加载Mongoose模块
9-
const mongoose = require('mongoose');
10-
11-
//数据库连接地址
12-
const dbURI = 'mongodb://127.0.0.1/my_database';
13-
14-
15-
//创建数据库连接
16-
mongoose.connect(dbURI);
17-
18-
// CONNECTION EVENTS
19-
//监听连接事件
20-
21-
// 当连接成功时
22-
mongoose.connection.on('connected', function () {
23-
console.log('Mongoose default connection open to ' + dbURI);
24-
});
25-
26-
// 如果连接失败
27-
mongoose.connection.on('error', function (err) {
28-
console.log('Mongoose default connection error: ' + err);
29-
});
30-
31-
// 当连接断开时
32-
mongoose.connection.on('disconnected', function () {
33-
console.log('Mongoose default connection disconnected');
3+
const mysql = require('mysql');
4+
const redis = require('redis');
5+
const Db = require('mongodb').Db;
6+
const Server = require('mongodb').Server;
7+
const PORT = 3000;
8+
9+
/*-------------------------mysql----------------------------------------*/
10+
const mysqlClient = mysql.createConnection({
11+
host: process.env.DB_HOST,
12+
user: process.env.MYSQL_USER,
13+
password: process.env.MYSQL_PASSWORD,
14+
database: process.env.MYSQL_DATABASE
3415
});
35-
36-
// 如果node进程结束,关闭连接
37-
process.on('SIGINT', function () {
38-
mongoose.connection.close(function () {
39-
console.log('Mongoose default connection disconnected through app termination');
40-
process.exit(0);
16+
mysqlClient.connect();
17+
async function getMysqlVersion() {
18+
let result = await new Promise((resolve, reject) => {
19+
mysqlClient.query('select version() as version', function (err, rows, fields) {
20+
if (err) return reject(err);
21+
resolve(rows);
22+
});
23+
}).catch((err) => {
24+
throw new Error(err);
25+
});
26+
return result;
27+
}
28+
29+
/*-------------------------mongodb---------------------------------------*/
30+
async function getMongoVersion() {
31+
const db = new Db('test', new Server(process.env.MONGO_HOST, 27017));
32+
let mongoClient = await new Promise((resolve, reject) => {
33+
db.open((err, db) => {
34+
if (err)return reject(err);
35+
resolve(db);
36+
});
37+
}).catch((err) => {
38+
throw new Error(err);
4139
});
42-
});
4340

41+
const adminDb = mongoClient.admin();
42+
let info = await new Promise((resolve, reject) => {
43+
adminDb.buildInfo(function (err, info) {
44+
if (err)return reject(err);
45+
resolve(info);
46+
});
47+
}).catch((err) => {
48+
throw new Error(err);
49+
});
50+
return info;
51+
}
52+
/*---------------------redis---------------------------------------------*/
53+
const redisClient = redis.createClient('6379', process.env.REDIS_HOST);
4454

55+
redisClient.on("error", (err) => {
56+
throw new Error(err);
57+
});
4558

46-
// uses async arrow functions
47-
app.use(async (ctx, next) => {
59+
/*-------------------------koa2----------------------------------------*/
60+
app.use(async(ctx) => {
4861
try {
49-
await next(); // next is now a function
50-
console.info(ctx);
51-
} catch (err) {
52-
ctx.body = { message: err.message };
53-
ctx.status = err.status || 500;
62+
let mysqlVersion = await getMysqlVersion();
63+
let redisVersion = redisClient.server_info.redis_version;
64+
let mongoVersion = await getMongoVersion();
65+
66+
ctx.body = `
67+
<h1>Dcoker+Koa2+MongoDB+Redis+MySQL</h1>
68+
<h4>Integrate development environment</h4>
69+
MySQL Version: ${mysqlVersion[0].version}<br />
70+
MongoDB Version: ${mongoVersion.version}<br />
71+
Redis Version: ${redisVersion}
72+
`;
73+
} catch (e) {
74+
ctx.status = 500;
75+
ctx.body = e.stack;
5476
}
5577
});
5678

57-
app.use(async ctx => {
58-
/// const user = await User.getById(ctx.session.userid); // await instead of yield
59-
60-
ctx.body = 'body'; // ctx instead of this
61-
});
62-
63-
app.listen(3000,function (err) {
64-
if (err){
65-
return err;
66-
}
67-
68-
console.log('http://127.0.0.1:3000');
79+
app.listen(3000, () => {
80+
console.info(`open http://127.0.0.1:${PORT}`);
6981
});

0 commit comments

Comments
 (0)