-
Notifications
You must be signed in to change notification settings - Fork 0
/
server3.js
85 lines (70 loc) · 2.12 KB
/
server3.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const express = require("express");
const path = require('path');
const fs = require('fs');
const mongoose = require('mongoose');
const app = express();
let foodLimit=6;
require('dotenv').config();
const MONGO_URL = process.env.MONGO_URL;
mongoose.connect(MONGO_URL, { useNewUrlParser: true });
const db = mongoose.connection;
//copied from https://mongoosejs.com/docs/index.html
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log('Connected to db');
// we're connected!
});
const recipeSchema = mongoose.Schema({
emotionType: String,
recipeName: String,
description: String,
ingredientsDetails: [String],
instructions: [String],
url: String,
imgUrl: String,
specialIngredient: {
name: String,
description: String,
source: String
}
});
const recipeModel = mongoose.model("recipeModel", recipeSchema);
console.log(recipeModel);
app.use(express.static("public"));
// app.use(express.static("assets"));
app.use(express.json());
function random(min, max){
return Math.floor(Math.random()*(max-1-min));
}
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "views/index.html"))
});
app.get("/about", (req, res) => {
res.sendFile(path.join(__dirname, "views/about.html"))
});
app.get("/recipes", (req, res) => {
recipeModel.find({}, (err, docs) => {
res.json(docs);
// console.log(recippe);
});
});
app.get("/recipes/anger", (req, res) => {
recipeModel.find({emotionType:"anger"}, (err, docs) => {
res.json(docs);
// console.log(recippe);
}).skip(random(0,18)).limit(foodLimit);
});
app.get("/recipes/sad", (req, res) => {
recipeModel.find({emotionType:"sad"}, (err, docs) => {
res.json(docs);
// console.log(recippe);
}).skip(random(0,20)).limit(foodLimit);
});
app.get("/recipes/stressed", (req, res) => {
recipeModel.find({emotionType:"stressed"}, (err, docs) => {
res.json(docs);
}).skip(random(0,18)).limit(foodLimit);
});
app.listen(process.env.PORT || 4000, () => {
console.log("Server is listening on port 4000!");
});