-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
51 lines (43 loc) · 1.37 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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const config = require('config');
const cors = require('cors');
const app = express();
const db = config.get('mongoURI');
const usersRoutes = require('./api/routes/users');
const categoriesRoutes = require('./api/routes/categories');
const servicesRoutes = require('./api/routes/services');
const reviewsRoutes = require('./api/routes/reviews');
// Connecting to database
mongoose
.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
})
.then(() => console.log('db connected'))
.catch((err) => console.log(err))
//set mongoose's Promise equal to global Promise since mongoose's Promise version is depricated
mongoose.Promise = global.Promise;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
// Routes to handle requests
app.use('/users', usersRoutes);
app.use('/categories', categoriesRoutes);
app.use('/services', servicesRoutes);
app.use('/reviews', reviewsRoutes);
app.use((req, res, next) => {
const error = new Error('Not Foud');
error.status = 404;
next(error);
});
//error handling middleware
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.json({
error: err.message
});
});
module.exports = app;