-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (36 loc) · 1.28 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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const placesRoutes = require('./routes/places-routes');
const usersRoutes = require('./routes/users-routes');
const HttpError = require('./models/http-error');
const app = express();
app.use(bodyParser.json());
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/places', placesRoutes);
app.use('/api/users', usersRoutes);
app.use((req, res, next) => {
const error = new HttpError('Could not found the route.', 404);
return next(error);
});
app.use((error, req, res, next) => {
if(res.headerSent) {
return next(error);
}
res.status(error.code || 500)
.json({message: error.message || 'An unknown error occured!'});
});
mongoose
.connect('mongodb+srv://rukshanjayasekara:[email protected]/travelSnapDB?retryWrites=true&w=majority')
.then(() => {
app.listen(5000);
console.log('Server listening on port 5000...');
})
.catch(err => {
console.log(err);
});