-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
63 lines (51 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
require('dotenv').config();
const express = require('express');
const exphbs = require('express-handlebars');
const cookieParser = require('cookie-parser');
const morgan = require('morgan');
const passport = require('passport');
const moment = require('moment');
const helmet = require('helmet');
const PORT = process.env.PORT || 3333;
const app = express();
const db = require('./models');
app.use(cookieParser());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
if (app.get('env') !== 'test') {
app.use(morgan('dev')); // Hook up the HTTP logger
}
app.use(express.static('public'));
require('./config/passport')(db, app, passport); // pass passport for configuration
// Define our routes
app.use('/api', require('./routes/apiRoutes')(passport, db));
app.use(require('./routes/htmlRoutes')(db));
// Secure express app
app.use(helmet.hsts({
maxAge: moment.duration(1, 'years').asMilliseconds()
}));
// catch 404 and forward to error handler
if (app.get('env') !== 'development') {
app.use((req, res, next) => {
const err = new Error('Not Found: ' + req.url);
err.status = 404;
next(err);
});
}
const syncOptions = {
force: process.env.FORCE_SYNC === 'true'
};
if (app.get('env') === 'test') {
syncOptions.force = true;
}
db.sequelize.sync(syncOptions).then(() => {
if (app.get('env') !== 'test' && syncOptions.force) {
require('./db/seed')(db);
}
app.listen(PORT, () => {
console.log(`App listening on port: ${PORT}`);
});
});
module.exports = app;