-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
45 lines (34 loc) · 1.28 KB
/
index.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 app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const routes = require('./backend/routes/dish.routes');
const path = require ('path');
//port
app.set('port', process.env.PORT || 3000);
//database
app.set('db', process.env.MONGODB_URI || 'mongodb://mongo:27017/myFavouriteDish')
mongoose.Promise = global.Promise;
//start ddbb
mongoose.connect(app.get('db'))
.then (()=> console.log('dataBase connected'))
.catch(err => console.log('dataBase is NOT connected' + err));
//middlewares
app.use(bodyParser.urlencoded({extended:false}));
// working through Json
app.use(bodyParser.json());
//setting Cors
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Request-Method');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
res.header('Allow', 'GET, POST, OPTIONS, PUT, DELETE');
next();
});
//routes
app.use(routes);
app.use(express.static(path.join(__dirname, 'dist')));
//start server
app.listen(app.get('port'), () =>
console.log('server running on port ' + app.get('port'))
)