We're going to learn how to create API endpoints and how to access them from the browser. Link our backend and our frontend app.
Let's send a GET request with query parameters http://localhost:3000/products?search=games&rating=5
.
Here is an example of we can get the query params via the req
object.
// web-server/src/app.js
//...
app.get('/products', (req, res) => {
const { search, rating } = req.query;
console.log('search: ', search);
console.log('rating: ', rating);
if (!search) {
// We need to use return here otherwise we res.send twice which throw an error!
// Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
return res.send({
error: 'You must provide a search term',
});
}
res.send({
product: [{}],
});
});
//...
Let's try it with our weather
endpoint – http://localhost:3000/weather?address=brussels
.
// web-server/src/app.js
//...
app.get('/weather', (req, res) => {
const { address } = req.query;
if (!address) {
return res.send({
error: 'You must provide an address!',
});
}
res.send({
location: 'Brussels-Capital, Belgium',
forecast:
'Partly cloudy in Eisingen (Belgium). It is currently 13 degrees out. It feels like 13 degrees out',
address,
});
});
//...
Time to link our work done with the weather-app and our web server.
// web-server/src/app.js
//...
app.get('/weather', (req, res) => {
const { address } = req.query;
if (!address) {
return res.send({
error: 'You must provide an address!',
});
}
geocode(address, (error, { lat, long, location } = {}) => {
if (error) {
return res.send({ error });
}
forecast(long, lat, (error, forecastData) => {
if (error) {
return res.send({ error });
}
res.send({
location,
forecast: forecastData,
address,
});
});
});
});
//...
// playground/7-default-params.js
const greeter = (name) => {
console.log(`Hello ${name}`);
};
greeter('Max'); // Hello Max
greeter(); // Hello undefined
const greeterImproved = (name = 'User', age) => {
console.log(`Hello ${name}`);
};
greeterImproved(); // Hello user
// ---
// Object destructuring (and default params)
const product = {
label: 'Red notebook',
price: 3,
stock: 201,
salePrice: undefined,
};
// const transaction = (type, { label, stock }) => {
// console.log(type, label, stock);
// };
// transaction('order'); // TypeError: Cannot destructure property 'label' of 'undefined' as it is undefined.
const transactionImproved = (type, { label, stock = 0 } = {}) => {
console.log(type, label, stock);
};
transactionImproved('order'); // order undefined 0
We explore the fetch API available only in the client side. Fetch allows us to fetch data from an url from the browser.
// web-server/public/js/app.js
console.log('Client side JS file is loaded!');
fetch('http://localhost:3000/weather?address=boston').then((response) => {
response.json().then((data) => {
if (data.error) {
console.log(data.error);
} else {
const { location, forecast } = data;
console.log(location);
console.log(forecast);
}
});
});
// web-server/public/js/app.js
const weatherForm = document.querySelector('form');
const search = document.querySelector('input');
weatherForm.addEventListener('submit', (e) => {
e.preventDefault();
const location = search.value;
fetch(`http://localhost:3000/weather?address=${location}`).then(
(response) => {
response.json().then((data) => {
if (data.error) {
console.log(data.error);
} else {
const { location, forecast } = data;
console.log(location);
console.log(forecast);
}
});
},
);
});