-
Notifications
You must be signed in to change notification settings - Fork 28
/
app.js
50 lines (42 loc) · 1.67 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
const path = require('path');
const express = require('express');
const mongoose = require('mongoose');
const keys = require('./configs/keys');
// Import Routes files
const todoRoutes = require('./routes/todos');
// Express settings and middlewares
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Routes
app.use('/api/todos', todoRoutes);
// Deploy with client sides
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
// Connect to Database
const MONGODB_URI = keys.mongoURI;
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true); // Deal with "DeprecationWarning: collection.ensureIndex is deprecated."
mongoose.connect(MONGODB_URI, { useNewUrlParser: true });
mongoose.connection.on('connected', () =>
console.log(`Database connected to: ${MONGODB_URI}`)
);
mongoose.connection.on('disconnected', () =>
console.log(`Database disconnected`)
);
mongoose.connection.on('error', err =>
console.log(`Database connect with error: ${err.message}`)
);
// Server
const PORT = process.env.PORT || 5566;
app.listen(PORT, () => {
console.log('┌──────────────────────────────────┐');
console.log('│ Todo List Server Started... │');
console.log(`│ Listening on the port ${PORT} │`);
console.log('│ (´・ω・`) │');
console.log('└──────────────────────────────────┘');
});