I recently came across this cool News API service called FeedRika that gives you the latest world news along with a sentiment score and relevant categories. It has a Free usage tier so I thought of trying it out and seeing what I can build with it.
One of my ideas was so build a tool to see how a company or topic has fared in the news.
You can see a chart from Google Trends that shows you how popular a term is in the public space but that only reflects the search volume. It does not give you an idea of whether the sentiment around it is positive or negative. So let's build a tool that scours the news to see if the topic is being written about favorably or not and display a similar graph.
Let's get an API key from the Feedrika website so we can fetch news articles to work with.
Head over to feedrika.com and sign up for an account.
Once you sign up you will find your API key on your profile page feedrika.com/profile along with your credit balance and a request log showing what requests you have made.
We could build this tool in just HTML, CSS and Javascript but it involves using a private API key and it's not a good idea to transmit that openly over the internet so let's use node and express to hide the API key on the server side as an environment variable and keep it private.
I'm going to tailor this tutorial to absolute beginners so if you're already familiar with node and express feel free to skip ahead to the more interesting parts.
Make sure you have the Node runtime environment installed. If not you can get it here.
Create a directory for this project on your local machine and navigate inside it.
Run : npm init -y in the terminal to initialize a node project with the defaults.
Run: npm i express to install the express framework.
Express is a simple webserver that will allow us to serve the pages and api routes within our application. It's easy to setup and widely used so finding help online and troubleshooting is easy.
Open up the folder in VSCode or your favorite IDE and look inside.
You should have a node_modules folder, a package.json file and a package-lock.json file.
Let's make an index page that welcomes users to our app
Make a new file 'welcome.html in' the root of your project. Fill it with just the basic information to get started
Welcome This is my news trends app!
Let's setup our first route and return this welcome.html page when someone opens the app
Create an 'index.js' file in the root of your app and import the express framework.
// Import the express framework express = require("express"); // tell node that we are creating an express app const app = express(); // define the port that the server will run on const port = 3000; // define the route for the index page app.get("/", (req, res) => { res.sendFile(__dirname "/welcome.html"); }); // Start the server and tell the app to listen for incoming connections on the port app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });
Let's test our progress.
From the terminal run node index.js. You should see a confirmation message saying the server is running
Click the link in the terminal or paste it into the browser to confirm that you can see the welcome page
Let's setup an environment variable to save our API key.
Create a new file '.env' in the root of your project.
Copy and paste your API key from the Feedrika profile page here
Let's also add a '.gitignore' file so we don't accidently upload this private key to the web
We don't want to start and stop the server from the terminal every time we make an edit to the app so let's setup auto reloading.
Open your package.json file and add these lines to the script object
"start": "node index.js", "dev": "nodemon index.js -w"
We are using nodemon with the '-w' flag to watch for changes in our root folder and restart the server.
Now we can start our server with the npm run dev command and it will automatically watch for changes and restart the server for us.
If you get an error about not recognizing nodemon run this to install it globally and try again:
npm i nodemon -g
Let's update the welcome page and add a search box to ask for topics
Welcome News trends
Search for a topic to get started
Create a 'public' folder in the root of your project that will host our client side javascript, css and image files.
Add a 'styles.css' file to the public folder and add some basic styles for the welcome page
styles.css:
/* Import the Inter font */ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap'); body { margin: 0; padding: 0; font-family: 'Inter', sans-serif; } #container { width: 100%; height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; } /* SEARCH FORM */ .search-form input { padding: 1em; border: 1px solid #ccc; border-radius: 8px; } .search-form button { padding: 1em; border: 1px solid #ccc; border-radius: 8px; background-color: #313131; cursor: pointer; color: #fff; }
Now we need to tell express how to serve these static files so open 'index.js' and add this line:
app.use(express.static("public"));
You should be able to see the changes reflected right away, refresh the page in your browser and confirm
If you notice the form it submits to a '/search' endpoint so let's setup this route and handle the form submission
Open up your 'index.js' file and add these lines
// define the route for the /search endpoint app.get("/search", (req, res) => { // get the query string from the request let query = req.query.topic; // send the query string back as the response res.send(query); });
Let's test it out, go to your browser and enter a search term in the box and click submit
You should see a response from the server which shows your search term, like this
Now that we have a search route working let's plug-in the FeedRika API and fetch news for the topic.
Coming soon Part II - Fetching Data
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3