Image for post
Image for post
SimpleDesktops

Advice From Chris

An advice engine powered by Node & Mongo.

So I was looking for a way to teach myself Node.JS and MongoDB, so I decided to write an API for an “advice engine”, somewhere to store quotes and display them on a lovely website.

My friend loves giving advice. Sometimes it’s really good advice, but sometimes it’s incredibly funny advice. So I wanted an API where my friends & I could add new quotes, and a website where quotes could be showcased and displayed :)

The API would be built in Node, with a MongoDB database, and the website would be a simple HTML5+JS page.No need to touch PHP.

Express & Mongoose

ExpressJS.

web application framework for node

Express makes handling HTTP requests in Node extremely simple, and has helped me pick up Node & it’s core concepts very quickly. If you’ve not looked at Express (or Node) at all, I strongly recommend you read the Express Getting-Started Guide :)

And Mongoose.

elegant mongodb object modeling for node.js

And that’s exactly what it is! There’s also a fantastic Getting-Started guide for Mongoose too! Thanks to Mongoose I’ve got an excellent data structure for these quotes, easy input and output, some excellent pre & post save hooks for timestamping, randomising and tweeting the quote when a new quote is added. All of which are in contained in a simple models.js.

Moment

A 5.5kb javascript date library for parsing, validating, manipulating, and formatting dates.

Moment is fantastic for parsing and displaying timestamps. It works in the browser and in Node, which makes it perfect for the frontend & the API!

I want to take Moment with me for all future applications using timestamps. It’s incredible effective and very efficient, and has a tiny footprint!

Tweetie

Image for post
Image for post
Bringing in Tweetie!

I finally managed to kick my brain into gear and work out how Twitter’s Streaming APIs works.

Using Twitter’s Public Streaming API and a magical tracking keyword “@AdviceFromChris”, any tweet that mentions the account can be processed and turned into a valid quote.

This makes Twitter an excellent source of content, and means any of my friends can add to this API by simply tweeting!

Filters - Authors

Image for post
Image for post
The userid check.

First set of filters control the author of the tweets. I only wanted my friends to be able to add to this (not anybody in the world) so an array of their IDs and a simple check is in place.

Filters - Content

Old-style RTs are great ways that’ll make the content dirty - I’m looking for content more like:

“Turn left at the roundabout, said no-one ever” @AdviceFromChris

Rather than:

RT @AdviceFromChris: “Turn left at the roundabout, said no-one ever”

Replies are much harder. They look like:

@AdviceFromChris Nice website dudes!

But what if someone is trying to send a quote this way? In order to get round this I’m insisting that replies have to be in quotations:

@AdviceFromChris “And yes, that is what she said.”

Image for post
Image for post
Introducing some crazy Regular Expressions!

So I needed some way to filter the text. Enter regular expressions.

These may look complicated, but really they’re a bunch of IF statements testing the content of the tweet, and seeing if it is valuable. If the content doesn’t match, then this function will return undefined and the code where this is called knows that if the resulting text is undefined then ignore this tweet.

Forever

A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)

This is really quite brilliant. It involves running a simple command to start running a script, it offers error logs and output logs (so you can monitor exactly what is going on with the server) and with a little manipulation I’ve got Forever handling everything, including archiving logs to a timestamped folder to keep a record of everything!

So what have I actually learned building this advice engine?

I got a taste at using Express & Mongoose in a real project (and not some project I knocked up at a hackathon!).

I’ve also tackled Twitter’s Streaming API - something I’ve always wanted to take on - and won. I really like the idea of using tweets as content (and not just relying on Twitter’s Search API and searching for hashtags - this here is a more managed and more long-term solution).

And I got a look at Forever - which I’ve now installed globally through npm and I’m using for other scripts that aren’t Node-based!

All of this code is on the GitHub repo for this project - and the actual site showcasing quotes is located in the gh-pages branch, since I’m powering the frontend by HTML5, JavaScript & GitHub Pages!

I’m kinda hoping that this project will inspire me to use Node.JS in more projects, over other languages I regularly use like PHP. I really like the idea of server-side JavaScript, and I really liked working on this project. It’s been fun! ^_^

Thank you for taking the time to read this article. This is my first Medium article, and I hope you enjoyed it! I’m looking at writing more, so please stay tuned :)

(Image courtesy of SimpleDesktops!)

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store