generated from kylewhittemore/react-sequelize-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
52 lines (45 loc) · 1.57 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
/* eslint-disable comma-dangle */
/* eslint-disable function-paren-newline */
const express = require('express');
const session = require('express-session');
const path = require('path');
const logger = require('morgan');
const bodyParser = require('body-parser');
const db = require('./models');
const routes = require('./routes');
const app = express();
const PORT = process.env.PORT || 8080;
// Serve static files from the React app build directory
app.use(express.static(path.join(__dirname, 'client/build')));
// Express boilerplate middleware
// =============================================
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Express session middleware
// =============================================
app.use(
session({ secret: 'keyboard cat', resave: true, saveUninitialized: true })
);
// Routing
// =============================================
app.use('/api', routes);
// Everything that is not an api request is sent to index.html
// for client side routing.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
// Sync sequelize models then start Express app
// =============================================
db.sequelize
.sync({ force: false })
.then(() => {
console.log('\n*************************************');
console.log(`${process.env.DB_NAME} database connected`);
})
.then(() => {
app.listen(PORT, () => {
console.log(`App listening on PORT ${PORT}`);
console.log('*************************************\n');
});
});