-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
103 lines (91 loc) · 2.46 KB
/
index.js
File metadata and controls
103 lines (91 loc) · 2.46 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
// Middleware
app.use(cors());
app.use(bodyParser.json());
// Connect to MongoDB (replace with your MongoDB URI)
mongoose.connect('mongodb://localhost:27017/food_recipes', {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log("Connected to MongoDB!");
}).catch((error) => {
console.error("Error connecting to MongoDB:", error);
});
// Recipe Schema
const recipeSchema = new mongoose.Schema({
name: { type: String, required: true },
ingredients: { type: [String], required: true },
instructions: { type: String, required: true },
cuisine: { type: String },
created_at: { type: Date, default: Date.now }
});
const Recipe = mongoose.model('Recipe', recipeSchema);
// Routes
// Get all recipes
app.get('/recipes', async (req, res) => {
try {
const recipes = await Recipe.find();
res.json(recipes);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Get a recipe by ID
app.get('/recipes/:id', async (req, res) => {
try {
const recipe = await Recipe.findById(req.params.id);
if (!recipe) {
return res.status(404).json({ message: "Recipe not found" });
}
res.json(recipe);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
// Add a new recipe
app.post('/recipes', async (req, res) => {
const { name, ingredients, instructions, cuisine } = req.body;
const newRecipe = new Recipe({
name,
ingredients,
instructions,
cuisine
});
try {
const savedRecipe = await newRecipe.save();
res.status(201).json(savedRecipe);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
// Update a recipe
app.put('/recipes/:id', async (req, res) => {
try {
const updatedRecipe = await Recipe.findByIdAndUpdate(
req.params.id,
req.body,
{ new: true }
);
res.json(updatedRecipe);
} catch (error) {
res.status(400).json({ message: error.message });
}
});
// Delete a recipe
app.delete('/recipes/:id', async (req, res) => {
try {
await Recipe.findByIdAndDelete(req.params.id);
res.status(204).send();
} catch (error) {
res.status(400).json({ message: error.message });
}
});
// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});