forked from Mohamedghaly140/Airbnb-ITI-Graduation--Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (62 loc) · 1.86 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
// Load env vars
require('dotenv').config({ path: './.env' });
const fs = require('fs');
const path = require('path');
const express = require('express');
const morgan = require('morgan');
const colors = require('colors');
const connectDB = require('./db/db');
const cors = require('cors');
const HttpError = require('./models/HttpError');
// Connect to database
connectDB();
const app = express();
// body parser middleware
app.use(express.json({ extended: false }));
// Serving statics images
app.use('/uploads/images', express.static(path.join('uploads', 'images')));
// Dev Logging Middleware
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
app.use(cors());
// import routes files
const placesRoutes = require('./routes/placesRoutes');
const usersRoutes = require('./routes/usersRoutes');
const authRoutes = require('./routes/authRoutes');
const bookingRoutes = require('./routes/BookingRoutes');
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, PUT, DELETE, OPTIONS'
);
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
// excute routes
app.use('/api/places', placesRoutes);
app.use('/api/users', usersRoutes);
app.use('/api/auth', authRoutes);
app.use('/api/booked_places', bookingRoutes);
app.use((req, res, next) => {
const error = new HttpError('Could not found this route', 404);
throw error;
});
app.use((error, req, res, next) => {
if (req.file) {
fs.unlink(req.file.path, err => {
console.log(err);
});
}
if (res.headerSent) {
return next(error);
}
res.status(error.code || 500).json({
message: error.message || 'An unknown error occured! Server Error',
});
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}...`.yellow.bold);
});