-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (45 loc) · 1.29 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
const express = require('express');
const helmet = require('helmet');
const cors = require('cors');
const mongoose = require('mongoose');
const path = require('path');
const fs = require('fs');
let env;
if (fs.existsSync(path.join(__dirname, './env'))) {
env = require(path.join(__dirname, './env'));
} else {
env = process.env;
}
// if (process.env.NODE_ENV !== 'production') {
// env = require(path.join(__dirname, './env'));
// } else {
// env = process.env;
// }
const server = express();
const corsOptions = {
// refactor
// origin: 'https://lambda-notes-app.netlify.com/',
origin: '*',
credentials: true,
};
const userRoutes = require('./api/routes/users');
const noteRoutes = require('./api/routes/notes');
mongoose.connect(env.DATABASE_URL)
.then(mongo => {
console.log(`Sucessfully connected to database ${env.DATABASE_URL}`)
})
.catch(err => {
console.log(`Error connecting to database: ${err}`)
})
// Middlewares
server.use(cors(corsOptions));
server.use(helmet());
server.use(express.json());
// Route Handlers
server.use('/api/v1', userRoutes);
server.use('/api/v1', noteRoutes);
const port = process.env.PORT || 5000;
if (process.env.NODE_ENV !== 'testing') {
server.listen(port, () => console.log(`\n=== API running on port ${port} ===\n`));
}
module.exports = server;