-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (49 loc) · 1.67 KB
/
server.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
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import morgan from 'morgan';
//import models and routes
import Recipe from './app/models/recipe';
import { getRecipes, getRecipe, postRecipe, deleteRecipe } from './app/routes/recipe';
//express server
const app = express();
const port = process.env.PORT || 8080;
//Mpmgoose DB connection
//options
const options = {
server: {socketOptions: {keepAlive: 1, connectTimeoutMS: 30000}},
replset: {socketOptions: {keepAlive: 1, connectTimeoutMS: 30000}}
};
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/cookbook', options);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
//body parser and Morgan middleware
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(morgan('dev'));
//static assets
app.use(express.static(__dirname + '/client/dist'));
// Enable CORS so that we can make HTTP request from webpack-dev-server
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,POST,DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// API routes
app.route('/recipes')
// create a recipes
.post(postRecipe)
// get all the recipes
.get(getRecipes);
app.route('/recipes/:id')
// get a single recipe
.get(getRecipe)
// delete a single recipe
.delete(deleteRecipe);
app.route("*").get((req, res) => {
res.sendFile('client/dist/index.html', { root: __dirname });
});
app.listen(port);
console.log(`listening on port ${port}`);