-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
34 lines (28 loc) · 1.15 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
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
require('dotenv').config({ path: './config/.env' });
const catchCallback = require('./helpers/errorHandling');
const app = express();
// Bodyparser Middleware
app.use(express.json());
mongoose
.connect(`mongodb+srv://${process.env.MONGODB_USER}:${process.env.MONGODB_PASS}@cluster0.fmalu.mongodb.net/xpense-tracker?retryWrites=true&w=majority`)
.then(() => console.log('MongoDB connected'))
.catch(catchCallback);
// Use Routes
app.use('/api/users', require('./routes/api/users'));
app.use('/api/lists', require('./routes/api/lists'));
app.use('/api/items', require('./routes/api/items'));
// Terminate All Routes
app.use('/api/terminate', require('./routes/api/terminate'));
// Serve static assets in PRODUCTION
if (process.env.NODE_ENV === 'production') {
// Set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`server started on ${port}`));