-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
127 lines (105 loc) · 3.67 KB
/
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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const bodyParser = require('body-parser');
const auth = require('./middleware/auth');
const path = require('path');
const cors = require('cors');
require('dotenv').config();
require('dotenv').config({ path: path.join(__dirname, `./.env.${process.env.NODE_ENV}`) });
conn_new_app = mongoose.createConnection('mongodb://localhost/new_app');
const multer = require('multer');
const upload = multer({ dest: './uploads/' });
const Image = require('./models/image')(conn_new_app);
const User = require('./models/user')(conn_new_app);
const Location = require('./models/location')(conn_new_app);
const login = require('./controllers/login');
const getSpots = require('./controllers/getSpots');
const getBookmarks = require('./controllers/getBookmarks');
const getBookmark = require('./controllers/getBookmark');
const createBookmark = require('./controllers/createBookmark');
const deleteBookmark = require('./controllers/deleteBookmark');
const getUserCreatedSpots = require('./controllers/getUserCreatedSpots');
const deleteSpot = require('./controllers/deleteSpot');
const getNotApprovedList = require('./controllers/getNotApprovedList');
const createSpot = require('./controllers/createSpot');
const getAdmins = require('./controllers/getAdmins');
const approveSpot = require('./controllers/approveSpot');
const getSpotOwner = require('./controllers/getSpotOwner');
const getUsers = require('./controllers/getUsers');
const createUser = require('./controllers/createUser');
app.use(bodyParser({ limit: '20mb' }));
const corsOptions = {
methods: ['POST', 'PUT', 'GET', 'OPTIONS'],
origin: '*',
allowHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization'],
};
app.use(cors(corsOptions));
app.use(auth);
app.get('/', async (req, res, next) => {
res.json({ message: 'hello world!' });
});
app.post('/login', (req, res, next) => {
login(req, res, next);
});
app.get('/getSpots', (req, res, next) => {
getSpots(req, res, next);
});
app.post('/getBookmarks', (req, res, next) => {
getBookmarks(req, res, next);
});
app.post('/getBookmark', (req, res, next) => {
getBookmark(req, res, next);
});
app.post('/createBookmark', (req, res, next) => {
createBookmark(req, res, next);
});
app.post('/deleteBookmark', (req, res, next) => {
deleteBookmark(req, res, next);
});
app.post('/getUserCreatedSpots', (req, res, next) => {
getUserCreatedSpots(req, res, next);
});
app.post('/deleteSpot', (req, res, next) => {
deleteSpot(req, res, next);
});
app.get('/getNotApprovedList', (req, res, next) => {
getNotApprovedList(req, res, next);
});
app.post('/createSpot', upload.array('photos', 4), (req, res, next) => {
createSpot(req, res, next);
});
app.get('/getAdmins', (req, res, next) => {
getAdmins(req, res, next);
});
app.post('/approveSpot', (req, res, next) => {
approveSpot(req, res, next);
});
app.post('/getSpotOwner', (req, res, next) => {
getSpotOwner(req, res, next);
});
app.get('/getUsers', (req, res, next) => {
getUsers(req, res, next);
});
app.post('/createUser', (req, res, next) => {
createUser(req, res, next);
});
app.use((error, req, res, next) => {
res.status(error.status || 500).send({
error: {
status: error.status || 500,
message: error.message || 'Internal Server Error',
},
});
});
mongoose
.connect(process.env.DB_HOSTNAME, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(console.log('connected to db!'))
.catch(err => console.log('Connection error', err));
app.listen(process.env.PORT, () => {
console.log(`Example app listening at http://${process.env.URL}:${process.env.PORT}`);
});