I'm feeling a little unsure about this, but I'm currently working on an express API app as part of a Codecademy exercise and I'm not quite sure which aspect of the app actually constitutes the API.
Essentially, there's a file that contains various quotes which are then displayed in the browser. Users can interact with these quotes by either getting a random one (by clicking a 'random quote' button), fetching all the available quotes, or even adding their own quote to the mix.
So, where does the API fit into all of this? I know that an API serves as a bridge between different software components, but I'm struggling to grasp how it operates within the context of this project. To give you a better idea, here's the code snippet for the 'fetch all quotes' functionality:
scripts.js
fetchAllButton.addEventListener('click', () => {
fetch('/api/quotes')
.then(response => {
if (response.ok) {
return response.json();
} else {
renderError(response);
}
})
.then(response => {
renderQuotes(response.quotes);
});
});
server.js
app.get('/api/quotes', (req, res) => {
let quoteMatch;
let quoteSearch = req.query.person;
if (quoteSearch == undefined) {
res.send({quotes: quotes})
} else {
quotesMatch = quotes.filter(quote => {
return quote.person == quoteSearch && quote;
});
if (quoteMatch) {
res.send({ quotes: quotesMatch });
} else {
res.status(404).send('Author not found!! 😡');
}
}
})
data.js
const quotes = [
{
quote: 'We build our computer (systems) the way we build our cities: over time, without a plan, on top of ruins.',
person: 'Ellen Ullman'
},
{
quote: 'The best thing about a boolean is even if you are wrong, you are only off by a bit.',
person: 'Anonymous'
},