-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (39 loc) · 1.33 KB
/
index.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 express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser')
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const userRoutes = require('./routes/user.js')
const app = express();
dotenv.config();
app.use(bodyParser.json({ limit: "30mb", extended: true }))
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }))
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.use(cors())
app.use(express.json())
app.use(cookieParser())
// Routes
app.use('/users', userRoutes)
app.get('/', (req, res) => {
res.send('Hello from backend')
})
// Setting up server
const PORT = process.env.PORT || 8080
app.listen(PORT, () => console.log(`Server is up and running on port: ${PORT}`))
// Connecting to mongoDB
const CONNECTION_URL = process.env.CONNECTION_URL
mongoose.connect(CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
}, (err) => {
if(err) return console.error(err);
console.log("Connected to MongoDB")
})