-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (47 loc) · 1.84 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
62
63
64
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const events = require('events');
const Database = require('./lib/database.js');
const Redis = require('./lib/redis.js');
// Allows us to broadcast events in our application
// This is not the only way to do this - you can use
// socket.io, for example.
const emitter = new events.EventEmitter();
// We pass the emitter to our redis class so we can
// use it to broadcast an event.
const redis = new Redis(emitter);
// We're listening for new messages
emitter.on('new-message', function(message) {
// While this works - make sure you check the requirements for Lab 4.
// Here, you will still need to insert the record into the SQLite database
console.log('New message!', 'Find me in ./server.js', message);
// Use sockets to talk to the front-end/client/dashboard.mustache
io.emit('new-message', message);
})
// Init database for later
let database = new Database('./api.db');
// include the mustache template engine for express
const mustacheExpress = require('mustache-express');
// registers the mustache engine with Express
app.engine("mustache", mustacheExpress());
// sets mustache to be the view engine
app.set('view engine', 'mustache');
// sets /views to be the /views folder
// files should have the extension filename.mustache
app.set('views', __dirname + '/views');
app.get('/dashboard', async function(req, res) {
let messages = await database.get_messages();
let data = {
title: "Dashboard",
items: messages,
}
res.render('dashboard', data);
});
app.get(/^(.+)$/, function(req,res) {
console.log("static file request: " + req.params[0]);
res.sendFile(__dirname + req.params[0]);
});
var server = http.listen(3000, function() {
console.log("server listening...");
});