forked from comp3370-5810/chat337
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.mjs
64 lines (50 loc) · 1.58 KB
/
app.mjs
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
import Koa from 'koa';
import Router from 'koa-router';
import { readFileSync, existsSync } from 'fs';
import { initSystem } from './server.mjs';
import koaBody from 'koa-body';
const PORT = process.env.PORT || 33333
const app = new Koa();
const router = new Router();
const chat = initSystem();
function replyWithFile(ctx, fileName, type) {
ctx.type = type;
if (existsSync(fileName)) {
ctx.body = readFileSync(fileName);
} else {
ctx.status = 404;
ctx.body = `File "${fileName}" not found.`;
ctx.type = '.txt';
}
}
router.get('/', async ctx => {
replyWithFile(ctx, './resources/chat.html', '.html');
});
router.get('/client.js', async ctx => {
replyWithFile(ctx, './client.js', '.js');
});
router.get('/image/:fileName', async ctx => {
replyWithFile(ctx, 'resources/' + ctx.params.fileName, '.jpg');
});
router.get('/style.css', async ctx => {
replyWithFile(ctx, './resources/chat.css', '.css');
});
router.get('/json/users', async ctx => {
ctx.type = 'application/json';
ctx.body = chat.getUsers();
});
router.get('/json/chats/:userId', async ctx => {
ctx.type = 'application/json';
ctx.body = chat.listAllContacts(ctx.params.userId);
});
router.get('/json/chat/:userId/:receiver', async ctx => {
ctx.type = 'application/json';
ctx.body = chat.allMessages(ctx.params.userId, ctx.params.receiver);
});
router.post('/json/send/:receiverId', koaBody(), async ctx => {
chat.sendMessage(ctx.request.body.sender, ctx.params.receiverId, ctx.request.body.message);
ctx.status = 201;
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(PORT);