forked from jatinparmar96/GuestMeBackend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
90 lines (74 loc) · 2.39 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
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const speakerRoutes = require('./src/routes/speaker');
const organizationRoutes = require('./src/routes/organization');
const reviewRoutes = require('./src/routes/review');
const { seedBookings, seedSpeaker, seedOrganization } = require('./seed/seed');
const bookingRoutes = require('./src/routes/booking');
const app = express();
app.use(cors());
const Schema = mongoose.Schema;
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' })); //parse form data
// 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('/speakers', speakerRoutes);
app.use('/organizations', organizationRoutes);
app.use('/reviews', reviewRoutes);
app.use('/bookings', bookingRoutes);
app.get('/', (req, res) => {
res.send('Hello GuesteaBackend');
});
app.use((error, req, res, next) => {
const statusCode = error.statusCode || 500;
const message = error.message;
const data = error.data;
res.status(statusCode).send({ message: message, data: data });
});
const MyModel = mongoose.model('Test', new Schema({ name: String }));
app.get('/seed', async (req, res) => {
// await remove();
// await seedSpeaker();
// await seedOrganization();
// await seedReviews();
await seedBookings();
res.json();
});
app.get('/seedJson', async (req, res) => {
if (req.query.type === 'speaker') {
res.json(await seedSpeaker(req.body));
}
if (req.query.type === 'organization') {
res.json(
await seedOrganization(Object.keys(req.body).length ? req.body : [])
);
}
});
app.get('/dbtest', async (req, res) => {
const model = new MyModel({});
model.name = new Date().toISOString();
model
.save()
.then((result) => {
res.send(result);
})
.catch((err) => {
console.log(err);
});
});
app.listen(process.env.PORT || 80, () => {
console.log(`🛫 Server ready at http://localhost:${process.env.PORT || 80}/`);
});
mongoose.connect(process.env.DATABASE_ACCESS, (result) =>
console.log('Database connected' + result)
);
// module.exports = mongoose.connection;