I spent last Friday in the mood of the innovation with a few other liipers. So we had all day long to explore a new technology, something we had never tried before. The main purpose of this hackday was to implement a simple REST API based on NodeJS providing a list of images taken randomly from the world wide web.
Our API should index images and return a list of (let's say 20) random images in JSON. At the end we will connect this API to a mobile app so we can swipe right and left to another random image.
First step with NodeJS
To get started, we chose the ExpressJS framework. The project is quite well documented and it didn't take long before we had the first page up and running.
We also chose to add MongoDB to our playground because it fits well in the NodeJS ecosystem.
Using what exists already
Node comes with its close friend, ‘npm' ( npmjs.org), the package manager. It has about 30'000 packages already shared on the platform, so all we had to do was to find the proper library and use it. In our case we looked for:
- a way to connect to Mongo
- a web parser
- a Flickr API connector
and we found the following packages:
- mongoose
- crawler
- flickr-js
Installing these dependencies is very quick and looks similar to Bundler or Composer.
- add those packages as dependency in ‘package.json'
- npm install
Now we just need to import them in the project with:
var mongoose = require('mongoose');
var FlickrJS = require('flickr-js');
var Crawler = require('crawler').Crawler;
That's it. Now we're ready for coding and indexing the images.
Rendering JSON has never been so easy with Express. If you return a javascript Hash as a response, then it's rendered automatically.
Short demo
On the left: The mobile app retrieve an image feed from our API, then we can swipe images right and left.
On the right: A simple admin panel to run image indexing.
Last word
We really enjoyed playing with the node ecosystem as it is very simple to get started with. And also for the good documentation we found along. It's also very convenient to have only one place for gathering node packages.
At the end of the day we got the general process:
- what do we want to do
- find the feature we want on npm
- implement it in our project
- code the bare minimum to make everything working