-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (39 loc) · 995 Bytes
/
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
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const irc = require('irc');
const db = require('./db');
const { broadcast } = require('./utils');
const client = new irc.Client('irc.hackint.org', 'Webuser69Wink', {
channels: ['#gpn'],
secure: true,
port: 6697
});
client.addListener('message', (from, to, text) => {
const message = {
date: new Date(),
text,
from
};
db.get('messages')
.push(message)
.write();
broadcast(expressWs, message);
});
client.addListener('error', error => console.error(error));
app.get('/messages', (req, res) => {
const messages = db
.get('messages')
.drop(parseInt(req.query.offset) || 0)
.take(parseInt(req.query.limit) || 100);
if (messages) {
res.json(messages);
} else {
res.status(404).json({
error: 'No messages matched your query.'
});
}
});
app.ws('/push', () => {});
app.use(express.static('public'));
app.listen(8080);