-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
67 lines (53 loc) · 2.11 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
require('dotenv').config();
// console.log(process.env)
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const fs = require('fs');
const path = require('path');
const postsRoutes = require('./routes/posts-routes');
const userRoutes = require('./routes/user-routes');
const collectionsRoutes = require('./routes/collections-routes');
const commentsRoutes = require('./routes/comments-routes');
const appointmentsRoutes = require('./routes/appointments-routes');
const reviewsRoutes = require('./routes/reviews-routes');
const HttpError = require('./models/http-error');
const app = express();
//? Parsing incoming body and extract json data
app.use(bodyParser.json());
//* Provide access to images, express.static() ==> just return it, don't process.
app.use('/uploads/avatars', express.static(path.join('uploads', 'avatars')));
app.use('/uploads/posts', express.static(path.join('uploads', 'posts')));
app.use('/uploads/default', express.static(path.join('uploads', 'default')));
//* CORES
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With , Content-Type, Accept, Authorization');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE');
next();
})
app.use('/api/posts', postsRoutes);
app.use('/api/user', userRoutes);
app.use('/api/collections', collectionsRoutes);
app.use('/api/comments', commentsRoutes);
app.use('/api/appointments', appointmentsRoutes);
app.use('/api/reviews', reviewsRoutes);
app.use((req, res, next) => {
next(new HttpError('Route not exist', 404));
return;
});
app.use((error, req, res, next) => {
if (req.file) {
fs.unlink(req.file.path, (err) => { console.log(err) });
}
if (res.headerSent) {
return next(error);
}
//* Set error code
res.status(error.code || 500);
res.json({ message: error.message || 'An error occurred!' });
})
mongoose
.connect(process.env.MONGODB_API)
.then(() => app.listen(process.env.PORT||5000))
.catch(err => console.log(err))