-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmain.js
75 lines (60 loc) · 1.97 KB
/
main.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
const express = require('express');
const jsonServer = require('json-server');
const multer = require('multer');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 8080;
// Set up JSON Server middleware
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();
// Set up multer for handling file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/');
},
filename: (req, file, cb) => {
cb(null, Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ storage });
// Ensure the uploads directory exists
if (!fs.existsSync('uploads')) {
fs.mkdirSync('uploads');
}
app.use(middlewares);
app.use('/uploads', express.static('uploads'));
// Custom route to handle multipart/form-data POST requests
app.post('/recipes', upload.single('image'), (req, res) => {
const db = router.db; // Get lowdb instance
// Extract form data from request
const { name, ingredients, instructions, cuisineId, dietId, difficultyId } = req.body;
// Create new recipe object
const newRecipe = {
id: String(Date.now()), // Generate a simple unique ID
name,
ingredients: ingredients ? ingredients.split(',') : [],
instructions,
cuisineId,
dietId,
difficultyId,
image: req.file ? `/uploads/${req.file.filename}` : null
};
// Save new recipe to the database
db.get('recipes').push(newRecipe).write();
res.status(201).json(newRecipe);
});
// Utility functions to get cuisine and difficulty names by ID
const getCuisineNameById = (id, db) => {
const cuisine = db.get('cuisines').find({ id }).value();
return cuisine ? cuisine.name : null;
};
const getDifficultyNameById = (id, db) => {
const difficulty = db.get('difficulties').find({ id }).value();
return difficulty ? difficulty.name : null;
};
// Routes
app.use('/', router);
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});