Search Gifs
In this step of the tutorial we will add the gif search functionality.
#
Change BranchesWhenever we want to make changes to our code we should do this on a new branch. Run the following command in your terminal.
git checkout -b add-gif-search
#
Add SearchRight now we aren't able to accept users' input. Let's add a form to our homepage that allows users to input search terms.
<form action="/" method="GET"> <input type="text" name="term" /> <button type="submit">Search</button></form>
Now our view shows a search bar but it doesn't do anything let's update our server to use it.
app.get("/", (req, res) => { let term = ""; if (req.query.term) { term = req.query.term; } console.log(term); res.render("home");});
Nice! You should see your search term in the terminal, whenever you submit the form.
#
Connect TenorYou might be wondering how we are going to get some gifs. Were going to scrape the entire internet for them... just kidding. A company called Tenor has an Application Programming Interface (API) that we can use. Head over to this link and get an API key. Once you have your API key store it somewhere safe, we are going to use it in a sec. We are going to use something called Node Fetch to make requests to Tenor's API. Let's get Node Fetch setup.
npm install node-fetch@2.6.7
const fetch = require("node-fetch");
We are also going to want to install the dotenv package. The dotenv package package will allow us to get variables from a .env
file. We want to store our API keys in a .env
file because we want to keep them private. Let's get dotenv setup.
npm install dotenv@16.0.0
require("dotenv").config();
touch .env
API_KEY=yourkey
Now we should have everything setup to get some gifs from Tenor. Let's make some reqests. We will be using the fetch function we imported earlier to make requests to Tenor. fetch()
makes a GET
request to a url and then we use the then()
function to use the data returned from the url.
app.get("/", (req, res) => { let term = ""; if (req.query.term) { term = req.query.term; } fetch( `https://g.tenor.com/v1/search?q=${term}&key=${process.env.API_KEY}&limit=10` ) .then((response) => response.json()) .then((data) => { const gifs = data.results; res.render("home", { gifs }); });});
Now we are sending gifs to the home view but we aren't using them. Let's update our home view to show the gifs. Handlebars has a for each loop that looks like {{#each}} {{/each}}
we will use this to display each gif.
{{#each gifs}}<div class="tenor-gif-embed" data-postid="{{this.id}}" data-share-method="host" data-width="300px" data-height="300px"> <a href="{{this.url}}"></a></div><script type="text/javascript" src="https://tenor.com/embed.js"></script>{{/each}}
Nice! If you go to http://localhost:3000/ you should see gifs appear and your gif search should be working.
#
Push To GitHubIn your terminal run the following commands, to push your current code to the add-gif-search branch on GitHub.
git add .
git commit -m "Added Gif Search"
git push origin add-gif-search
You should see a link in your terminal following Create a pull request for 'add-gif-search' on GitHub by visiting:
. Hold CMD
and click the link. You will be brought to a pull request called Add Server. Press the button that says Create Pull Request
then press the Merge Pull Request
button. This will merge the add-gif-search branch with your main branch. In order to get the up to date main branch, locally, enter the following commands in your terminal.
git checkout main
git pull