Create a simple API with nodejs , and consume it with vanilla javascript
this a step-by-step tutorial to create your first simple api and consume it , this is a very simple tutorial and it doesn’t do anything complex ,it’s for complete beginners
The APP we are going to build together
Prerequisites:
have basic understanding of html , css and javascript,node
visual code studio
First Step: folder structure
install nodejs if you don’t have it in your machine already, checkout this link https://nodejs.org/en/
create a directory with name of nodeproductsapp
create these files index.html , script.js , style.css , server.js
create a folder of name data and inside of it, create a new file products.json that will contain our data
[{
"id":0,
"productName":"air pods 2022",
"image":"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxEREhAQDxAOEBAQEBAQEBAPDw8NDw8PFREXFxURFRYYHSggGB0lGxUVITIjJSkrLi4vFx8zOjMsNygtMSsBCgoKDg0NDw0NDisZFRkrKysrLSs3LS0rKysrNysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrK//AABEIAOEA4QMBIgACEQEDEQH/xAAcAAEAAgMBAQEAAAAAAAAAAAAAAwUCBAYBBwj/xAA+EAACAQICBgYIAwYHAAAAAAAAAQIDEQQFEiExQVFxBiIyYYGxBxNCUnKRocEU0eEjJDNiosIIU2OCkrLw/8QAFgEBAQEAAAAAAAAAAAAAAAAAAAEC/8QAFhEBAQEAAAAAAAAAAAAAAAAAAAER/9oADAMBAAIRAxEAPwD7iAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHkpW2gegidR8vqzy74suCYEWk+L8Ueqpx+hMEgPE7noAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB5KVtZrTn8/IyxFS3h5mupFiNiCJUQQkTRYGVjxxMgRUTTRnCd+Z62RyRRMDCnK/MzIAAAAAAAAAAAAAAAAAAAAAAAAAAAqsdW61ubI3VS2sizV2qc19zUesqLahVT2M5f0l9KKmCo0adB6NbEymlOybp04JaTjffeUV4vfYuqCcJLhLV47j5v6bKv7bAr3aNeX/KcF/YgOewvSjGUpqrTxWI007/ALSrOrCXdKMm1JfnuPvWR5h+Jw2H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2ajp4Hfax3MLstVQBl6P1N9denpmbD0kQWA14zM1WHLWm07lqPgkG5RPC4Vfujum0WHSQO0gd95IbL2VUrjNSXOoNiwZbX4XJ36ziLRwuHAIuOkTr3JofVqQ4LbuJlQwvJGubElF7WN/wqR75d9lYU0qSUyQSosSN28n5wN2IiAiIgIiICIiAiIgIiICYnoKd6iZYgRtbZCN0sPbeRlfk232ag9olliBRq+wK4+zfsMj6uEdfSpsPZOkzyQDvEDmJmvUwVN/Spoe1R8bTp1XA029Kmp9k0qnJ+gdylewwOObU2bTWtlCZV82WAGguLyt1EXzZc6a20Om7rBneq/JOk/pANwzKDbsM1k5DYQG5wtE9OqKfcRA5/yd5IhqSVGqN9IiPlygFc6g2vfW3Gwljo8mKY35z2kD4AS809mqNLCZxglgUynsCkP+mD23b4mZa+xKbo9M0wFcFWygKbHgRuPXLgMKvCZFpKNyiBy3D+SrDhsy1MQt/3qZ39bUzLtye5OphaXmqZbLmLEuQzEm17kADoHRLBEDGtICe5+xAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQP//Z",
"productPrice":1600
},
{
"id":2,
"productName":"iphone 13",
"image":"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBw8QDxUODxAODw0PEA8NDw4NDQ8PDQ0NFxYWFhURFhUYHSggGBomGxYVITEtJSorLi4uGB8zOjMuNygtLisBCgoKDg0OGxAQFzUfHyArLTcrLTctKystLS0rKy0uLS0tLS0vLS0rLSstLS0tLS0tKy0tLS0wLS0tLS0rLSstLf/AABEIAPQAzgMBIgACEQEDEQH/xAAcAAEAAgMBAQEAAAAAAAAAAAAAAQUDBAcCBgj/xABIEAACAQIBBggHDQYHAAAAAAAAAQIDEQQFBhIhMXETMkFRYXKRsQcUInSBktEWIzNCUlNUk5ShwcPSJjZVYqLwFRckc3XT4f/EABoBAQADAQEBAAAAAAAAAAAAAAABAgMEB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1ZdxU09i3ItsV8HLqS7iop7FuRS7bE9gAo1SQAEgAAlAgASaOOflLd+LN00MoPylu/FmmHnC+LnCabM8GacJGxCR6LpvVtRZ7TNeMjIpFZhhNXPcZ++WF8wf55105Bi3+2OF8xf5x1847cpcN/cABCgAAAAAAADFi/g59SXcyop7FuRb4t+9zvrWhLVsvqZUQ2Lcil22J6JPJJRqkEAJSCABIIAEldlJ+Ut34ssCvym1pJctr3vyXeqxfFzhph5wwwZmhI1osyRZ6MS7rQ2oyMikaqke1Isxmr4bEP9sML5j/3HYTjlSS92GFur/6K221n79rOxnDflLy8vKQAFWYAAAAAAADxVhpRcflJx7UUdB+TZ7Y+RJc0lqZfmjjMBpPTg9GfLqvGfNdc5W0bXpbTSJPHBV724CT6VUp2f3k8HW+Zl69P2lNS366+XoHng63zM/Xh7RoVvmZ+vD2jUnXXy9A86Fb5mfrw9o4Ot8zL14e0ak66+XoEcHW+Zl68PaJU61tVGTfKtOmrfeNSdVfKSqxk9Ko7bIrR9O1nrKdPKMoOOGoQp1GmlUxE4yjDp0YO8u1HxHuHzk/irXLqoxSNMfadytTNWlt+77FI9I+M9w+cn8Vf1UR7h85P4q/qonRGarf/ALa+P8fbImVRRTlJqMYpylKTSjGK2tt7EfE+4fOX+Kv6qBC8FmUsXJQyllPFVMPqcqVNQjCTun8trk+SW+vHhWfV1+IY8w5/4nnFXylTu8HhKSwtKpZ2nLVrT5uO90o852gq83cg4bAUI4bDQVOnHm2yfK2+Vloc8zudy4LW6p2AAhUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB//9k=",
"productPrice":1000
}]
so then folder structure would be something like this
Second step : html file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<Link href="./style.css" rel="stylesheet"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>tech Products</title>
</head>
<body>
<main >
<h1>Tech Products</h1>
<div id="productsContainer">
</div>
</main>
<script src="script.js"></script>
</body>
</html>
copy paste this code to your html file , we will add the content using javascript .
Third Step : css file
/* general style*/
body {
background-color: #161c24;
font-family: Arial, Helvetica, sans-serif;
color: white;
}
/************************************************/
/* header style */
main h1{
width: 100%;
text-align: center;
margin-bottom: 40px;
}
/*************/
/* Products Container */
#productsContainer{
display: flex;
justify-content: center;
align-items: center;
gap:20px;
flex-wrap: wrap;
}
/* Product Card Style*/
.productCard{
display: flex;
flex-direction: column;
align-items: center;
width: 256px;
height: 367px;
background-color: #212b36;
border-radius: 30px;
}
.imageContainer{
margin: 10px;
width: 240px;
height: 240px;
border-radius: 15px;
overflow: hidden;
}
.imageContainer img{
width:100%;
height: 100%;
}
.contentContainer{
width: 90%;
}
.contentContainer h1{
width: 100%;
text-align: left;
}
.contentContainer button{
width:80px;
height: 40px;
color: white;
background-color: #00ab55;
border: 1px solid #00ab55;
border-radius: 10px;
cursor: pointer;
font-weight: bold;
}
.price{
display: flex;
justify-content: space-between;
align-items: center;
}
this is style ,doesn’t need any explanation
fourth step: script.js file
// get main tag
const productsContainer = document.getElementById("productsContainer");
// return product Card Structure html + css classes
const getProductCardStructure = (productName, productPrice, productImage) => {
const ProductCardSection = document.createElement("section");
ProductCardSection.classList.add("productCard");
ProductCardSection.innerHTML = `
<div class="imageContainer">
<img src=${productImage} />
</div>
<div class="contentContainer">
<div>
<h1>${productName}</h1>
</div>
<div class="price">
<button>Buy Now</button>
<h2>$${productPrice}</h2>
</div>
</div>
`;
return ProductCardSection;
};
// add product cards to dom
const addCardProductTodDom = (productsList) => {
for (let product of productsList) {
productsContainer.appendChild(
getProductCardStructure(
product.productName,
product.productPrice,
product.image
)
);
}
};
// get products data from local server
const getProductsData = async () => {
const data = await fetch("http://127.0.0.1:5000/data");
const productsList = await data.json();
return productsList;
};
getProductsData().then((productsList) => addCardProductTodDom(productsList));
Explanation:
so now I will explain script.js file line by line
const productsContainer = document.getElementById("productsContainer");
so first we get products container element (we declare it in our html file) that will wrap all product cards
const getProductCardStructure = (productName, productPrice, productImage) => {
const ProductCardSection = document.createElement("section");
ProductCardSection.classList.add("productCard");
ProductCardSection.innerHTML = `
<div class="imageContainer">
<img src=${productImage} />
</div>
<div class="contentContainer">
<div>
<h1>${productName}</h1>
</div>
<div class="price">
<button>Buy Now</button>
<h2>$${productPrice}</h2>
</div>
</div>
`;
return ProductCardSection;
};
what this function basically do is it get product name ,price and image then we create our productcard using createElement function then we append our html code that contains data of product and css classes to our productCard that we created and return it
// add product cards to dom
const addCardProductTodDom = (productsList) => {
for (let product of productsList) {
productsContainer.appendChild(
getProductCardStructure(
product.productName,
product.productPrice,
product.image
)
);
}
};
all what this function is doing is it takes an array of products objects ,then add it to our products container
// get products data from local server
const getProductsData = async () => {
const data = await fetch("http://127.0.0.1:5000/data");
const productsList = await data.json();
return productsList;
};
the rule of this function is to get products data from our local server
getProductsData().then((productsList) => addCardProductTodDom(productsList));
in this line we call our function that gets our data ,and it’s an async function so we have to wait until it finish executing then after that we take that data and pass it to our “addCardProductTodDom”
that will add it to our DOM
fifth step: server.js file
const http = require("http");
const fs = require("fs");
const ProductsData = fs.readFileSync("./data/products.json");
const server = http.createServer((req, res) => {
const pathName = req.url;
// data path
if (pathName === "/data") {
// solve cors policy bug and specify type of response data
res.writeHead(200, {
"Access-Control-Allow-Origin": "*",
"Content-type": "application/json",
});
res.end(ProductsData);
}
// not found path
else {
res.end("not found path");
}
});
// listen to server
server.listen(5000, () => {
console.log("listening on port 5000");
});
Explanation
const http = require("http");
const fs = require("fs");
in these two lines we import http module that we will use to create our server,,and fs module we will need it read data from our “productsdata.json” file
const ProductsData = fs.readFileSync("./data/products.json");
we read our productsdata using readFileSync method of fs module
const server = http.createServer((req, res) => {
const pathName = req.url;
// data path
if (pathName === "/data") {
// solve cors policy bug and specify type of response data
res.writeHead(200, {
"Access-Control-Allow-Origin": "*",
"Content-type": "application/json",
});
res.end(ProductsData);
}
// not found path
else {
res.end("not found path");
}
});
in this block of code we use createServer Method that will fire with every request the server gets
and basically what it does is check if the path name is “/data” if true ,it will return the products data we read earlier using fs module ,if not it will return “not found path”
// listen to server
server.listen(5000, () => {
console.log("listening on port 5000");
});
in this line we start our server in port 5000
Final Result
now to see the final result ,go to your terminal in vs code and type “node server.js”
you should see something like this
then open your index.html file in chrome ,you should get this result