how to start writing API request
Edrick Ee
Posted on August 14, 2021
In order to request data from API, we need to set up an express to do get request
npm install express
create file name index.js for my main server foler
const express = require('express')
to call expressconst db = require('../databasefolder')
call db folder & filestart express by writing
const app = express();
const port = 1004;
port can be any 4 digit numbers
app.listen(port, () => console.log(
Successfully connected to the port ${port}
))write your get, put, post requests
ex.
app.get('/products', (req, res) => {
let { page, count } = req.query;
if (page === undefined) {
page = 0;
};
if (count === undefined) {
count = 5;
};
db.getProducts(page, count, (err, result) => {
if (err) {
res.status(400)
console.log(err);
}
res.status(200)
res.send(result);
})
});
app.get('/products/:product_id', (req, res) => {
const { product_id } = req.params;
db.getCurrentProduct(product_id, (err, result) => {
if (err) {
res.status(400)
console.log(err);
}
res.status(200)
res.send(result);
})
});
đź’– đź’Ş đź™… đźš©
Edrick Ee
Posted on August 14, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.