Notes Api
Notes Api
Through APIs, parts of data can be got from a different application to another application,
Ex: Insta uses FB’s API to access to the data of friends and display on their APIs
APIs are basically a contract b/w the data providers and developers.
Basically, these are all the things that developers can access, and these are the methods , the
objects, the protocols to access them. And we as the website that host the data will try not to
change any of these methods or functions without notifying you.
Ex: Tinder is using an external API to interact with the FB system and databases.
Through APIs, we can interact with the menu of the data and interact with it.
Every API that interacts with an external system, will have an endpoint.
The websites that have APIs tend to have more data, than just a bunch of random quotes.
The paths and parameters can be used to specifically narrow down any piece of data from
any external server.
To provide flexibility, APIs allow to provide parameters, which need to be put at the end of the url.
Monetizing and limiting the use of the data is necessary for the APIs to set.
API keys can be used to make requests, when to make any requests to the APIs
The first parameter gets added after a ‘?’, and the subsequent parameters gets added after ‘&’
When a request is made, the data that we get is in the JSON format
JSON is so reqdily used is because not only it’s in a format that can be easily read by a human, but
because it can be easily collapsed down to take up as little space as possible.
JSON is much lighter weight and easy to turn into
Getting data from APIs:
The client browser, or our user is gonna be typing in the web address into their browser , and that is
going to make a request to our server. That is going to be a GET request, so that’s going to get the
HTML, CSS, JS from our servers.
At this point, our server should return all the data – html, css, js. That is going to be a reponse.
But in orde to be able to give them that response, that include the data from someone else’s server,
we have to make a request. This is also a GET request, so they are going to give a piece of response
in the form of data that we need.
This is all done via the API, so via the menu they provided. They are going to specify what are the
parameters to specify, so to get the data and incorporate into the files.
WeatherProject:
To get the weather data, we need to make a GET request to the OpenWeather maps server, and
need to fetch the data as a JSON and parse it to get relevant data
1. HTTPS ----------Native
2. Request -----------------------------------
3. SuperAgent ------------------------------ External
4. Axios --------------------------------------
5. Got ----------------------------------------
Through this https native module, a GET request can be made across the internet using the https
protocol.
How to parse JSON:
JSON.parse() – converts a json into some string format. Hexadecimal or binary to text and then turn
it into an actual javascript object.
Example:
//jshint esversion:6
response.on("data", function(data) {
const weatherData = JSON.parse(data);
console.log(weatherData);
// const object = {
// name: "Umang",
// favFood: "Bul-Dak"
// }
// console.log(JSON.stringify(object));
const temp = weatherData.main.temp;
console.log(temp);
app.listen(3000, function() {
console.log("Server running on port 3000");
});
Res.send() is the end, no multiple sends can be given in the app.get() method.
Using Body Parser to Parse POST requests to the server:
//jshint esversion:6
app.use(bodyParser.urlencoded({extended: true}));
res.sendFile(__dirname + "/index.html");
});
response.on("data", function(data) {
const weatherData = JSON.parse(data);
const temp = weatherData.main.temp;
const weatherDesc = weatherData.weather[0].description;
const icon = weatherData.weather[0].icon;
const imageURL = "http://openweathermap.org/img/wn/" + icon +
"@2x.png";
app.listen(3000, function() {
console.log("Server running on port 3000");
});
News Letter Project:
The CSS and the image need to be rendered by putting remote location on the html file, rather than
local link
app.use(express.static("public"));
A name of the folder has to be kept, that is going to be kept as a static folder.
Now, by referring to a path to the static files by using app.use(express.static()) by a relative url,
which in this case is the “public folder”. Now these css and image files can be accessed.
const data = {
members: [
{
email_address: email;
status: "subscribed";
merge_fields: {
FNAME: firstName,
LNAME: lastName
}
}
]
};
After providing the url, options should be provided which will be a JS object.
Now specify the method as ”POST” , and next for this post to go through successfully, is to provide
some authentication