-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (51 loc) · 1.45 KB
/
server.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
const Koa = require('koa');
const Router = require('koa-router');
const static = require('koa-static');
const koaBody = require('koa-body');
const path = require('path');
const Content = require('./model/Content');
const consola = require('consola');
const Socket = require('socket.io');
const http = require('http');
const render = require('koa-ejs');
const app = new Koa();
const router = new Router();
const PROP = 2293;
const STATIC_PATH = path.join(__dirname, 'public');
consola.info(STATIC_PATH);
app.use(koaBody());
render(app, {
root: STATIC_PATH,
layout: 'template',
viewExt: 'html',
cache: false,
debug: true
});
app.use(router.routes()).use(router.allowedMethods());
app.use(static(STATIC_PATH));
router.get('/', async (ctx, next) => {
const points = await Content.content();
await ctx.render('content', {
points: JSON.stringify(points)
});
});
router.get('/api', async (ctx, next) => {
console.time('api');
ctx.body = await Content.content();
console.timeEnd('api');
});
const server = http.createServer(app.callback());
const io = Socket(server);
io.on('connection', function (socket) {
consola.success('收到了一个链接=>', socket.id);
socket.on('dot', async function (dot) {
await Content.content(dot);
io.emit('dot', dot);
});
socket.on('disconnect', function () {
consola.error('断开了一个链接=>', socket.id);
});
});
server.listen(PROP, () => {
consola.success(`http://0.0.0.0:${PROP}/`);
});