-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (60 loc) · 2.1 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
const fs = require('fs');
require('dotenv').config();
const cors = require('cors');
const logger = require('./services/logger');
const url = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.wpxhvxd.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const tasksRoutes = require('./routes/tasks-routes');
const transactionsRoutes = require('./routes/transactions-routes');
const usersRoutes = require('./routes/users-routes');
const projectsRoutes = require('./routes/projects-routes');
const commentsRoutes = require('./routes/comments-routes');
const projectMembersRoutes = require('./routes/project-members-routes');
const HttpError = require('./models/http-error');
const app = express();
app.use(bodyParser.json({ limit: '100mb' }));
app.use(bodyParser.urlencoded({extended: true}));
const corsOptions = {
"origin": "*",
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 200,
"exposedHeaders": ['Content-Length', 'X-Requested-With', ' Authorization','Content-Type'],
}
app.use(cors(corsOptions))
app.use('/project-tasks', tasksRoutes);
app.use('/projects', projectsRoutes);
app.use('/projects', commentsRoutes);
app.use('/users', usersRoutes);
app.use('/project-members', projectMembersRoutes);
app.use('/transactions-list', transactionsRoutes);
app.use((req, res, next) => {
const error = new HttpError('Could not find 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);
res.json({ message: error.message || 'An unknown error occurred!' });
});
mongoose
.connect(
url,
{ useNewUrlParser: true, useUnifiedTopology: true }
)
.then(() => {
app.listen(process.env.PORT || 5000);
logger.info(`server starts at ${process.env.PORT || 5000}`)
})
.catch(err => {
console.log(err);
});