-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (47 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
46
47
48
49
50
51
52
53
54
55
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const mongoose = require('mongoose');
const dbConfig = require('./db/database');
const cors = require('cors');
// Set up DB
mongoose.Promise = global.Promise;
mongoose.connect(dbConfig.url, {
useNewUrlParser: true
}).then(() => {
console.log('Database connected.');
}).catch(error => {
console.log('Database could not be connected: ' + error);
});
// Set up express
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cors());
// API root
const recipeRoute = require('./routes/recipe.routes')
app.use('/api/recipe', recipeRoute)
const interactionRoute = require('./routes/interaction.routes')
app.use('/api/interaction', interactionRoute)
// Port
const port = 8080;
// Start server
const server = app.listen(port, () => {
console.log('Recipe server listening at: ' + port)
})
// Find 404 and hand over to error handler
app.use((req, res, next) => {
res.status(404).json({
message: "Invalid route."
});
});
// Index route
app.get('/', (req, res) => {
res.status(404).json({
message: "Invalid route."
});
});
// Static build location
app.use(express.static(path.join(__dirname, 'dist')));