Automate Your Culinary Collection
If you’re a cooking enthusiast, chances are you have a recipe handbook — perhaps a treasured family heirloom or a personal collection of your culinary creations. While flipping through physical pages has its charm, it can be challenging to keep recipes organized, easily accessible, and shareable in today’s fast-paced digital world. Converting your recipe handbook into a digital format through a Recipe API not only preserves these precious recipes but also makes them more functional and versatile.
Imagine having your entire recipe collection stored digitally, where you can effortlessly search by ingredients, cuisine type, or cooking time. With a Recipe API, you can achieve this and more — creating a digital repository that is accessible from any device, anytime, and anywhere.
We’ll use the Recipe Management API as an example.
Recipe Management API
Step 1: Setting Up the Environment
- Install Node.js: Download and install Node.js from nodejs.org. This will also install npm (Node Package Manager).
- Create a Project Directory: Create a new directory for your project and navigate into it.
mkdir recipe-api
cd recipe-api
- Initialize a New Node.js Project: Run the following command to create a
package.json
file, which will manage your project's dependencies.
npm init -y
Step 2: Installing Necessary Packages
Install Express and other required packages.
npm install express body-parser
Step 3: Designing the API
The API will have the following endpoints:
GET /recipes
- Retrieve a list of recipesGET /recipes/:id
- Retrieve a specific recipe by IDPOST /recipes
- Add a new recipePUT /recipes/:id
- Update an existing recipeDELETE /recipes/:id
- Delete a recipe
Step 4: Implementing the API
Create a new file named app.js
and add the following code to implement the API:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
app.use(bodyParser.json());
let recipes = [
{ id: 1, title: 'Spaghetti Carbonara', ingredients: ['spaghetti', 'eggs', 'bacon', 'cheese', 'pepper'], instructions: 'Boil pasta, cook bacon, mix with eggs and cheese, combine.' },
{ id: 2, title: 'Chicken Curry', ingredients: ['chicken', 'curry powder', 'onions', 'tomatoes', 'coconut milk'], instructions: 'Cook chicken, add spices and vegetables, simmer with coconut milk.' }
];
// Retrieve a list of recipes
app.get('/recipes', (req, res) => {
res.json(recipes);
});
// Retrieve a specific recipe by ID
app.get('/recipes/:id', (req, res) => {
const recipe = recipes.find(r => r.id === parseInt(req.params.id));
if (!recipe) return res.status(404).send('Recipe not found');
res.json(recipe);
});
// Add a new recipe
app.post('/recipes', (req, res) => {
const recipe = {
id: recipes.length + 1,
title: req.body.title,
ingredients: req.body.ingredients,
instructions: req.body.instructions
};
recipes.push(recipe);
res.status(201).json(recipe);
});
// Update an existing recipe
app.put('/recipes/:id', (req, res) => {
const recipe = recipes.find(r => r.id === parseInt(req.params.id));
if (!recipe) return res.status(404).send('Recipe not found');
recipe.title = req.body.title;
recipe.ingredients = req.body.ingredients;
recipe.instructions = req.body.instructions;
res.json(recipe);
});
// Delete a recipe
app.delete('/recipes/:id', (req, res) => {
const recipeIndex = recipes.findIndex(r => r.id === parseInt(req.params.id));
if (recipeIndex === -1) return res.status(404).send('Recipe not found');
recipes.splice(recipeIndex, 1);
res.status(204).send();
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Step 5: Running the API
Start the server by running the following command in your terminal:
node app.js
Your API should now be running on http://localhost:3000
.
Step 6: Testing the API with Postman
1. GET /recipes
- Open Postman: Open the Postman application.
- Set Request Type to GET: Select
GET
from the dropdown menu. - Enter URL: Type
http://localhost:3000/recipes
. - Send Request: Click
Send
to see the list of recipes.
2. GET /recipes/
- Open Postman: Open a new tab in Postman.
- Set Request Type to GET: Select
GET
from the dropdown menu. - Enter URL: Type
http://localhost:3000/recipes/1
(replace1
with the desired recipe ID). - Send Request: Click
Send
to see the specific recipe details.
3. POST /recipes
- Open Postman: Open a new tab in Postman.
- Set Request Type to POST: Select
POST
from the dropdown menu. - Enter URL: Type
http://localhost:3000/recipes
. - Go to the Body Tab: Select the
Body
tab below the URL bar. - Select raw and JSON: Select
raw
and chooseJSON
. - Enter JSON Data:
{
"title": "Beef Stew",
"ingredients": ["beef", "potatoes", "carrots", "onions", "gravy"],
"instructions": "Brown beef, add vegetables, simmer with gravy until tender."
}
- Send Request: Click
Send
to add a new recipe.
4. PUT /recipes/
- Open Postman: Open a new tab in Postman.
- Set Request Type to PUT: Select
PUT
from the dropdown menu. - Enter URL: Type
http://localhost:3000/recipes/1
(replace1
with the recipe ID you want to update). - Go to the Body Tab: Select the
Body
tab below the URL bar. - Select raw and JSON: Select
raw
and chooseJSON
. - Enter JSON Data:
{
"title": "Beef Stew Updated",
"ingredients": ["beef", "potatoes", "carrots", "onions", "gravy"],
"instructions": "Brown beef, add vegetables, simmer with gravy until tender."
}
- Send Request: Click
Send
to update the recipe.
5. DELETE /recipes/
- Open Postman: Open a new tab in Postman.
- Set Request Type to DELETE: Select
DELETE
from the dropdown menu. - Enter URL: Type
http://localhost:3000/recipes/1
(replace1
with the recipe ID you want to delete). - Send Request: Click
Send
to delete the recipe.
By following these steps, you can create and test a Recipe Management API using Node.js and Express, and interact with it using Postman.
Happy Coding!! 😁😁