API Calls with HTTP Client Service
Posted By : Prince Kumar | 31-Oct-2018
Recently Angular 4.3 has introduced the new HttpClient service that has replaced the old Http Service that was used earlier in the Angular 2 version.
Most web applications communicate with backend services over the HTTP protocol. The modern browsers support different APIs for making HTTP requests:
1) The XMLHttpRequest interface
2) The fetch() API.
The HttpClient in @angular/common/HTTP offers a simplified client hypertext transfer protocol API for Angular applications that rests on the XMLHttpRequest interface exposed by browsers.
Additional edges of HttpClient include testability options, typed request and response objects, request and response interception, observable APIs, and streamlined error handling.
Advantages of HTTP Client Service :
It is easy to use, simple, and more powerful for making HTTP requests.
The back-end exposes the following endpoints:
They return an array in JSON format consisting all the Book, Movie, or Food Objects.
Apart from this, we also have the below-mentioned APIs:
CODE - HERE
module.exports = app
const express = require(
'express'
);
const bodyParser = require(
'body-parser'
);
const path = require(
'path'
);
const app = express();
app.use(express.
static
(__dirname));
app.use(bodyParser.json()); // support json encoded bodies
var foods = [
{
"id"
:
1
,
"name"
:
"Donuts"
},
{
"id"
:
2
,
"name"
:
"Pizza"
},
{
"id"
:
3
,
"name"
:
"Tacos"
}
];
var books = [
{
"title"
:
"Hitchhiker's Guide to the Galaxy"
},
{
"title"
:
"The Fellowship of the Ring"
},
{
"title"
:
"Moby Dick"
}
];
var movies = [
{
"title"
:
"Ghostbusters"
},
{
"title"
:
"Star Wars"
},
{
"title"
:
"Batman Begins"
}
];
// the
"index"
route, which serves the Angular-2 application
app.get(
'/'
, function (req, res) {
res.sendFile(path.join(__dirname,
'/dist/index.html'
))
});
app.get(
'/api/books'
, function (req, res) {
res.send(books);
});
// the GET
"movies"
API endpoint
app.get(
'/api/movies'
, function (req, res) {
res.send(movies);
});
<p>app.get(
'/api/food'
, function (req, res) {
res.send(foods);
});
// POST endpoint for creating a new food
app.post(
'/api/food'
, function (req, res) {
// calculate the next ID
let id =
1
;
if (foods.length >
0
) {
let max = Math.max.apply(Math, foods.map(function (f) { return f.id; }));
id = max +
1
;
}
let new_food = {
"id"
: id,
"name"
: req.body.name};
foods.push(new_food);
res.send(new_food);
});</p>
app.put(
'/api/food/:id'
, function (req, res) {
let id = req.params.id;
let f = foods.find(x => x.id == id);
f.name = req.body.name;
res.send(f);
});
app.delete(
'/api/food/:id'
, function (req, res) {
let id = req.params.id;
let f = foods.find(x => x.id == id);
foods = foods.filter(x => x.id != id);
res.send(f);
});
// HTTP listener
app.listen(
3000
, function () {
console.log(
'Example listening on port 3000!'
);
});
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Prince Kumar
Prince is a sharp and intellectual UI Developer, he has a good knowledge of HTML. CSS, Jquery. His hobbies are reading books, listening music and net surfing,