-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (67 loc) · 1.88 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
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
|--------------------------------------
| Dependencies
|--------------------------------------
*/
// Modules
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const methodOverride = require('method-override');
const cors = require('cors');
// Config
const config = require('./server/config');
/*
|---------------
| MongoDB
|---------------
*/
mongoose.connect(
config.MONGO_URI, { useNewUrlParser: true }
);
const monDb = mongoose.connection;
monDb.on('error', function () {
console.error('MongoDB Connection Error. Please make sure that', config.MONGO_URI, 'is running');
});
monDb.once('open', function callback() {
console.info('Connected to MongoDB:', config.MONGO_URI);
});
/*
|--------------------------------------
| App
|--------------------------------------
*/
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(cors());
// set port
const port = process.env.PORT || '8083';
app.set('port', port);
// set static path to Angular app in dist
// Don't run in dev
if (process.env.NODE_ENV !== 'dev') {
app.use('/', express.static(path.join(__dirname, './dist')));
}
/*
|--------------------------------------
| Routes
|--------------------------------------
*/
// register routes
require('./server/api')(app, config);
// Pass routing to Angular app
// Don't run in dev
if (process.env.NODE_ENV !== 'dev') {
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, '/dist/index.html'));
});
}
/*
|--------------------------------------
| Server
|--------------------------------------
*/
app.listen(port, () => console.log(`Server running on localhost:${port}`));