-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
87 lines (80 loc) · 2.38 KB
/
index.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const Koa = require('koa');
const path = require('path');
const views = require('koa-views');
const serve = require('koa-static');
const session = require('koa-session');
const mongoose = require('mongoose');
const bodyParser = require('koa-bodyparser');
const marked = require('marked')
let redisStore = require('koa-redis');
const flash = require('./middlewares/flash')
const router = require('./routes');
const error = require('./middlewares/error_handler')
const config = require('./config/config');
mongoose.connect(config.mongodb, { useNewUrlParser: true, useCreateIndex: true, useFindAndModify: false, useUnifiedTopology: true });
let figlet = require('figlet');
const chalk = require('chalk');
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false
})
const app = new Koa();
app.keys = [config.app_key];
let resources = require('./public/mix-manifest.json')
app.use(async (ctx, next) => {
ctx.state.ctx = ctx
ctx.state.marked = marked
ctx.state.resources = resources
ctx.state.domain = config.domain
await next()
})
app.use(views(path.join(__dirname, 'views'), {
map: { html: 'nunjucks' }
}));
app.use(serve(
path.join(__dirname, 'public')
));
app.use(session({
key: config.session.key,
maxAge: config.session.maxAge,
store: redisStore({
auth_pass: config.redis_password,
db: config.redis_db_session
})
}, app));
app.use(bodyParser());
app.use(error())
app.use(flash());
router(app);
let server;
server = app.listen(process.env.PORT, async () => {
await figlet('HELLO', (err, data) => {
if (err) {
return;
}
console.log(chalk.blue(data))
console.log(chalk.underline.yellow(`server is running at http://127.0.0.1:${process.env.PORT}`))
});
});
let onLineNum = 0;
let io = require('socket.io')(server);
io.on('connection', function (socket) {
onLineNum++
socket.broadcast.emit('open', {num: onLineNum})
socket.on('disconnect', function() {
onLineNum--
socket.broadcast.emit('close', {num: onLineNum})
});
socket.on('get_num', function (data) {
socket.emit('set_num', {num: onLineNum})
});
socket.on('news', function (data) {
socket.broadcast.emit('news_posts', { msg: `${ data.userName } 发布了新文章!`, userName: data.userName});
});
});