-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
38 lines (29 loc) · 917 Bytes
/
app.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
const express = require('express');
const mongoose = require('mongoose');
const authRoutes = require('./routes/auth');
const crudRoutes = require('./routes/crud');
const app = express();
const MONGODBURL = "<Your mongodb URL goes here>";
app.use(express.json());
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
app.use(authRoutes);
app.use(crudRoutes);
app.use((error, req, res, next) => {
console.log(error);
res.status(error.statusCode || 500).json({
message: error.message,
data: error.data
});
});
mongoose.connect(MONGODBURL)
.then(result => {
app.listen(3000);
})
.catch(error => {
console.log(error);
});