-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
70 lines (51 loc) · 1.99 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
61
62
63
64
65
66
67
68
69
'use strict';
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
//modules
const accountsMethods = require('./modules/accounts');
const recipesMethods = require('./modules/recipes');
const customRecipesMethods = require('./modules/customrecipes');
//Recipe methods
const getRecipes = recipesMethods.getRecipes;
const getOneRecipe = recipesMethods.getOneRecipe;
//Custom recipe methods
const getCustomRecipe = customRecipesMethods.getCustomRecipe;
const updateCustomRecipe = customRecipesMethods.updateCustomRecipe;
const createCustomRecipe = customRecipesMethods.createCustomRecipe;
const deleteCustomRecipe = customRecipesMethods.deleteCustomRecipe;
//Account methods
const createAccount = accountsMethods.createAccount;
const getAccount = accountsMethods.getAccount;
//List methods
const saveRecipe = accountsMethods.saveRecipe;
const removeRecipe = accountsMethods.removeRecipe;
// add validation to confirm we are wired up to our mongo DB
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log('Mongoose is connected');
});
const app = express();
app.use(cors());
app.use(express.json());
const PORT = process.env.PORT || 3001;
//test if server is working
app.get('/test', (request, response) => {
response.send('test request received');
});
app.get('/recipes', getRecipes);
app.get('/recipes/:id', getOneRecipe);
app.get('/customrecipes/:id', getCustomRecipe);
app.put('/customrecipes/:id', updateCustomRecipe);
app.post('/customrecipes', createCustomRecipe);
app.delete('/customrecipes/:id', deleteCustomRecipe);
app.post('/accounts', createAccount);
app.get('/accounts/:email', getAccount);
app.put('/accounts/list/save/:email', saveRecipe);
app.put('/accounts/list/remove/:email', removeRecipe);
app.get('*', (request, response) => {
response.send('No resource found');
});
app.listen(PORT, () => console.log(`listening on ${PORT}`));