-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (46 loc) · 1.62 KB
/
app.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
require('dotenv').config();
const path = require('path');
const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const db = require('./database/models');
const apiRoutes = require('./routes/api');
const webpack = require('webpack');
const webpackConfig = require('./webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const compiler = webpack(webpackConfig);
const app = express();
const env = process.env.NODE_ENV;
const port = process.env.PORT;
app.use(express.static(path.join(__dirname, 'dist')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/api', apiRoutes(express.Router()));
if (env === 'development') {
app.use(webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
stats: {
modules: false,
},
}));
}
app.use((req, res) => {
return res.status(404).send('not found');
});
app.get('*', (req, res, next) => {
if (env === 'development') {
const filename = path.resolve(compiler.outputPath, 'index.html');
return compiler.outputFileSystem.readFile(filename, (err, result) => {
if (err) return next(err);
return res.set('content-type','text/html').send(result).end();
});
}
return res.sendFile(path.resolve(__dirname, 'dist') + '/index.html');
});
app.set('port', port);
const server = http.createServer(app);
db.sequelize.authenticate()
.then(() => server.listen(port, () => console.log(`env: ${env}\nport: ${port}`)))
.catch(() => console.log('sequelize error'));