-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
45 lines (43 loc) · 1.32 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Created by EX_WLJR_CHENYULUN on 2017/11/1.
*/
// const qiniu = require('./lib/qiniu');
module.exports = app => {
app.beforeStart(function* () {
// 应用会等待这个函数执行完成才启动
app.logger.info('app启动');
// app.cities = yield app.curl('http://example.com/city.json', {
// method: 'GET',
// dataType: 'json'
// });
});
// app.config.coreMiddleware.unshift('errorHandler');
app.validator.addRule('jsonString', (rule, value) => {
try {
JSON.parse(value);
} catch (err) {
return 'must be json string';
}
});
// qiniu(app);
// set redis session store
// session store must have 3 methods
// define sessionStore in `app.js` so you can access `app.redis`
app.sessionStore = {
async get(key) {
const res = await app.redis.get(key);
if (!res) return null;
return JSON.parse(res);
},
async set(key, value, maxAge) {
// maxAge not present means session cookies
// we can't exactly know the maxAge and just set an appropriate value like one day
if (!maxAge || maxAge === 'session') maxAge = app.config.session.maxAgeRides || 24 * 60 * 60 * 1000;
value = JSON.stringify(value);
await app.redis.set(key, value, 'PX', maxAge);
},
async destroy(key) {
await app.redis.del(key);
}
};
};