forked from rethinkdb/angular2-typescript-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
64 lines (53 loc) · 1.62 KB
/
app.ts
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
/// <reference path="./typings/node/node.d.ts"/>
var r = require("rethinkdb");
var sockio = require("socket.io");
var http = require("http");
var app = require("koa")();
var router = require("koa-router")();
var config = {
database: {
host: "localhost",
db: "chat"
},
port: 8000
};
app.use(require("koa-bodyparser")());
app.use(require("koa-static")(`${__dirname}/public`));
app.use(router.routes());
router.post("/api/messages/create", async function() {
var conn = await r.connect(config.database);
this.body = await r.table("messages").insert({
user: this.request.body.user,
text: this.request.body.text,
time: r.now(),
}).run(conn);
conn.close();
});
router.get("/api/messages", async function() {
var conn = await r.connect(config.database);
this.body = await r.table("messages")
.orderBy({index: r.desc("time")})
.limit(100).orderBy("time")
.run(conn);
conn.close();
});
var server = http.createServer(app.callback());
var io = sockio(server);
(async function() {
var conn = await r.connect(config.database);
try {
await r.dbCreate(config.database.db).run(conn);
await r.tableCreate("messages").run(conn);
await r.table("messages").indexCreate("time").run(conn);
}
catch (err) {
if (err.message.indexOf("already exists") < 0)
console.log(err.message);
}
(await r.table("messages").changes().run(conn)).each((err, item) => {
if (item && item.new_val)
io.sockets.emit("message", item.new_val);
});
})();
server.listen(config.port,
() => console.log(`Server started on port ${config.port}.`));