To Enable Weather Realtime based on your location , please allow location access.
Ever Wondering why API Created ? Perhaps this is the right topic for you to get started
img : wallpapercave-server
Oh hey , before to get started , you may that wanted to practice and learning , here a repo for you to practice
And the thing that save your history chat , log or something will be required a backend or Database .
The API will take care as the bridge between them.
an API will be used as bridge between front-end and back-end , But you can say that an API is a part of Server-side (A.K.A Backend)
API is special that can allowed you change the data overtime . Here the list what can API do .
Lib : Node JS and Express
$ npm initentry point: (index.js)$ npm install express --savego to localhost:3000
The Plan :
index.js
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");const app = express();
const PORT = process.env.PORT || "3000";const booksRoute = require("./api/books");
const hotelsRoute = require("./api/hotels");// Use middleware
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());// Handle http requests
app.get("/", (req, res) => {
return res.send("Requesting API On Standby"); {/_ This will given the notice when everything success _/}
});// Add additional routes
app.use("/books", booksRoute);
app.use("/hotels", hotelsRoute);// Start the server
app.listen(PORT, () => console.log(Running on port ${PORT}
));exports = module.exports = app;
It’s where the object will be called
books.js
const express = require("express");
const router = express.Router();
const fs = require("fs");// to string . or stringify
const booksData = JSON.parse(
fs.readFileSync("./api/booksData.json").toString()
);
// const booksData = JSON.parserequire('./api/booksData.js')// define the home page route
router.get("/", function (req, res) {
res.json(booksData);
});router.get("/:bookId", (req, res) => {
console.log(Looking for data ${req.params.bookId}
);
const book = booksData.filter((item) => {
return item.card === req.params.bookId;
});
if (book != "") {
res.send(book);
} else {
res.status(404).send("Book not found");
}
});// params
router.get("/title/:query", (req, res) => {
// TODO urldecode query
const books = booksData.filter((item) => {
return item.title.toLowerCase().includes(req.params.query.toLowerCase());
}); if (books.length > 0) {
res.send(books);
} else {
res.status(400).send("No Results");
}
});// define the about route
router.get("/about", function (req, res) {
res.send("About Books");
});module.exports = router;
It’s an Object , you can fill anything but you might need an interactive state like GET and DELETE
booksData.json
[ { "card": "9781593275846", "title": "test1", "subtitle": "", "nama": "name1", "hari": "Day", "lokasi": "Indonesia", "waktu": "18.00-20.00", "description": "", "infoLink": "website description", "url": "website" }, { "card": "9781593275846", "title": "test1", "subtitle": "", "nama": "name1", "hari": "Day", "lokasi": "Indonesia", "waktu": "18.00-20.00", "description": "", "infoLink": "website description", "url": "website" } ]
Possible things :
var port = process.env.PORT || 3000;
router.get("/", function (req, res) {
res.json(booksData);
});
var express = require('express')
var app = express()app.get('/', function (req, res) {
res.send('Hello World from Backend !')
})app.listen(3000)
Making an API through Backend like Express can be so trivia , it can be easy or a nightmare
But not to worry , the community will be there for you . If not you can talk with leader or the team that you can depends on .
So Congratulations for making an API~!