initial commit

This commit is contained in:
fred 2025-07-08 10:40:49 -07:00
commit cd234531b9
37 changed files with 5529 additions and 0 deletions

63
backend/server.js Normal file
View file

@ -0,0 +1,63 @@
const express = require("express");
const db = require("./db");
const port = 6063;
const cors = require('cors'); // to remove cors origin error in dev TODO: remove when dockerized
const app = express();
app.use(cors()); // to remove cors origin error in dev TODO: remove when dockerized
app.use(express.json());
// routes
app.get("/recipes", async (req, res) => {
try {
const recipes = await db('recipes').select('id', 'name');
res.json(recipes);
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
app.get("/recipe/:id", async (req, res) => {
const id = req.params.id
try {
const recipe = await db('recipes').where('id', '=', id).select('id', 'name', 'cuisine').first();
res.json(recipe);
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
app.post("/add-recipe", async (req, res) => {
const { name, cuisine } = req.body;
try {
const [id] = await db('recipes').insert({
name: name,
cuisine: cuisine
}, ['id'])
res.status(200).send({ message: "Recipe added", id: id.id });
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
app.delete("/delete-recipe", async (req, res) => {
const { id } = req.body;
try {
await db('recipes').where({ id: id }).del();
res.status(200).send({ message: "Recipe deleted" });
} catch (err) {
console.log(err);
res.status(500).json({ error: err.message });
}
});
app.listen(port, () => console.log(`Server has started on port: ${port}`));
process.on('SIGINT', async () => {
console.log('Closing database connection...');
await db.destroy();
process.exit(0);
});