This repository has been archived by the owner on Feb 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
97 lines (84 loc) · 2.33 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const express = require('express');
const session = require('express-session');
const cors = require('cors');
const mongoose = require('mongoose');
const routes = require('./backend/routes/index');
const bodyParser = require('body-parser');
const { ErrorHandler,
handleError } = require('./backend/helpers/errors');
// so that you can use .env
const path = require('path');
require('dotenv').config();
// for routing
const app = express();
// get environment variables
const {
PORT = 5000,
NODE_ENV = 'development',
SESS_NAME = 'sid',
SESS_SECRET,
USER,
PASSWORD
} = process.env
// configure express-session
const TWO_HOURS = 1000 * 60 * 60 * 2;
app.use(session({
name: SESS_NAME,
resave: false,
saveUninitialized: false,
secret: SESS_SECRET,
cookie: {
maxAge: TWO_HOURS,
sameSite: true,
secure: false
}
}))
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, 'client/build')));
// assign database name depending on environment
let db_name;
let uri;
switch(NODE_ENV){
case 'production':
db_name = 'habits_db';
uri = `mongodb+srv://${USER}:${PASSWORD}@cluster0-c71bf.mongodb.net/${db_name}`;
break;
case 'development':
db_name = 'habits_db_dev';
uri = `mongodb://localhost:27017/${db_name}`;
break;
case 'test':
db_name = 'habits_db_test'
uri = `mongodb://localhost:27017/${db_name}`;
break;
default:
throw new Error('cannot find process env')
}
// MongoDB connection
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true }
);
const db = mongoose.connection;
db.on('error', (error) => console.error(error));
db.once('open', () => console.log('Connected!!!'));
// routes for api
app.use('/api/v1', routes);
// routes for frontend
app.get('*', (req, res) =>{
res.sendFile(path.join(__dirname+'/client/build/index.html'));
});
//error handling
app.use((req, res, next) => {
let err = new ErrorHandler(404, 'Not Found');
next(err)
});
app.use((err, req, res, next) => {
handleError(err, res)
});
// start express server
app.listen(PORT, () => {
console.log(`Habit is running on port: ${PORT}`);
});
module.exports = {app : app, uri : uri};