A project for HN fanatics
Posted by Daniele Zanni on May 4, 2015
Firebase. A reason to love the HN APIs is that they are on Firebase. Since there is an npm module for firebase, it has never been so easy to use a web API.
Email.js. I did not know this npm module before, but after playing a bit with it, I cannot think of a better way to send an email from your io.js/node.js application. You will only need an SMTP server available (I use Gmail’s one for this project).
Git. Because it is 2015 and there is absolute no excuse for not to use a Version control systems (VCS).
These are the basic setup steps to create your JS on HN Emailer project:
You should click Enter until it is done.
Now it is time to install the external module dependencies that we are going to use in our project:
$ npm install firebase emailjs --save
Finally, we only need to init git in our folder, before beginning to actually write some code:
$ git init
##Let’s start coding: filtering the HN posts
We want to write our code in a new file called index.js:
$ touch index.js
Open it with your favorite text editor.
Using Firebug with the Hacker News APIs allows us to get notified every time a new post is added to Hacker News:
Run your index.js file with node index.js. If you wait enough time, you should now see your terminal logging the ID of every new post published on Hacker News.
We can now use this ID to retrieve the information about the specific post.
Let’s create a function that does exactly that:
If we run again our application, we should see our terminal logging the details of each new post (and any update made to it).
Let’s create a filterInterestingPosts function, which we will use as a callback for dealWithSinglePost.
If everything went fine, we should now start to see our application logging if each new post matches our Regular Expression.
Your code up to this point should look like like this:
##Adding Email.js to send your emails
I use Gmail to send my emails, but you can use your favorite SMTP server. In any case you will probably need to have some credentials to send your email.
Let’s create a config file which won’t be shared in your VCS. Add the node_modules and config.json manually to .gitignore if you don’t have printf in your SO):
Open your config.json file and add a JSON object containing your SMTP credentials and your email:
You can now import Email.js and config.json at the beginning of your index.js file and setup your SMTP server object: