-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
68 lines (56 loc) · 1.48 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
68
const express = require('express');
const path = require('path');
const swig = require('swig');
swig.setDefaults({ cache: false });
const db = require('./db');
const { Day, Hotel, Restaurant, Activity, Place } = db.models;
const app = express();
app.set('view engine', 'html');
app.engine('html', swig.renderFile);
app.use(require('body-parser').json());
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use('/vendor', express.static(path.join(__dirname, 'node_modules')));
let config = process.env;
try {
config = require('./env.json');
}
catch(ex){
}
app.use(function(req, res, next){
res.locals.GOOGLE_API_KEY = config.GOOGLE_API_KEY;
next();
});
app.use('/days', require('./routes/days'));
app.get('/', (req, res, next)=> {
const options = {
include: [ Place ]
}
Promise.all([
Hotel.findAll(options),
Restaurant.findAll(options),
Activity.findAll(options)
])
.then(([ hotels, restaurants, activities ])=> {
res.render('index', { hotels, restaurants, activities });
})
.catch(next);
});
app.use((req, res, next)=> {
const error = new Error('page not found');
error.status = 404;
next(error);
});
app.use((err, req, res, next)=> {
res.status(err.status || 500).render('error', { error: err });
});
const port = process.env.PORT || 3000;
db.sync()
.then(()=> db.seed())
.then( result => {
//console.log(result);
})
.then(()=> {
app.listen(port, ()=> {
console.log(`listening on port ${port}`);
});
});