forked from Sagacious-Sycamore/hrgotchi
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
67 lines (57 loc) · 1.59 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
65
66
67
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var cors = require('cors');
var db = require('./data/database.js').db;
var controller = require('./server/controller.js');
var poll = require('./server/worker.js').poll;
//start sequelize database
db.authenticate()
.then(function(err) {
if (err) {
throw error;
} else {
console.log('Database started.');
}
})
.catch(function(err) {
console.log('error connecting', err);
});
//start express
var app = express();
//use bodyparser middleware
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({'extended':false}));
app.use(bodyParser.json());
app.use(cors());
//use cookieparser & session
app.use(cookieParser('cookies!!!'));
app.use(session({
secret: 'What\'s your secret?',
resave: false,
saveUninitialized: true
}));
//log requests
app.get('/log', controller.getLog);
//api requests
app.get('/api/pet', controller.get);
app.post('/api/pet', controller.post);
app.post('/api/newPet', controller.new);
app.get('/api/test', function(req, res) {
console.log('Testing polling function...');
poll();
res.end();
});
//login and logout routes
app.get('/logout', controller.logout);
app.post('/login', controller.login);
app.post('/signup', controller.signup);
//index route
app.get('/*', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
// Uncomment to poll database reguarly
setInterval(poll, 5000);
app.listen(3000);
console.log('Server listening on 3000...');