-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (28 loc) · 991 Bytes
/
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
const express = require('express')
const mongoose = require('mongoose')
const morgan = require('morgan')
const bodyParser = require('body-parser')
const fs = require('fs')
const cors = require('cors')
require('dotenv').config()
//app
const app = express()
const port = 5000;
// db
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.mc16x.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`
mongoose.connect(uri, {useNewUrlParser: true, useCreateIndex: true,
useFindAndModify: false, useUnifiedTopology: true})
.then(() => console.log('DB CONNECTED'))
.catch(err => console.log(`DB CONNECTION ERR${err}`))
// middlewares
app.use(morgan("dev"));
app.use(bodyParser.json({limit:'2mb'}));
app.use(cors());
//routes middleware
fs.readdirSync('./routes').map((r) =>
app.use( '/api', require('./routes/' +r))
)
//port
app.listen(process.env.PORT || port,() => {
console.log(`Example app listening at http://localhost:${port}`)
})