Back to the Backend Era

Part 5 — A Simple Matter of Programming

Blogging by Anonymous Collective, Creative Commons Attribution License.
In this series of articles we explore not what we can do with serverless application platforms, but what we no longer have to do.

Previous parts:

In Parts 1, 2, 3 and 4, we chose the platform, operating system, programming language and the database for our backend, and we installed everything that is needed to run our code. This time we will actually write that code.

Back to the drawing board

Now is a good time to focus on what are we actually building, because now we finally have a place to build it. It is a simple service with clear requirements that, like most simple things, is going to get complicated beyond recognition pretty soon. But it is still simple now. We need to build a website that shows some news. Those news items are going to change, they are dynamic, and that’s why we need a backend to serve fresh news to our frontend.

The news items are going to have a title, author, content and publication date. We don’t expect to need any more data, which is to say that we are going to need some unexpected data later, but not just yet.

We need a simple news collection in the database. We run the Mongo shell, then we create a new database “backendera” and create a new user with read-only access with the syntax of the Mongo shell that we will soon memorize:

user: "newsreader",
pwd: "secure1",
roles: [{role: "read", db: "backendera"}]

In that database, we create a new collection that we call “news”:


And we insert three news items:{
title: "News1",
author: "Author1",
content: "This is news number 1",
date: new Date()
title: "News2",
author: "Author2",
content: "This is news number 2",
date: new Date()
title: "News3",
author: "Author3",
content: "This is news number 3",
date: new Date()

After verifying that we have the correct data in the correct collection, we can close the Mongo shell and start coding our backend.

We create a new Node project with npm and install the mongodb module. The others options are mongo, mongojs, mongo-native, mongoose and few others. But after carefully examining all pros and cons of those modules let’s say we decided to go for mongodb.

The installation fails, but we soon realize that “fatal error: gssapi/gssapi.h: No such file or directory” means, of course, that we need to install the libkrb5-dev system package. We try to install it again and it works like a charm. We can start coding now. Finally.

So we start our app.js from:

var mongo = require('mongodb').MongoClient;
var url = 'mongodb://newsreader:secure1@localhost:27017/backendera';

and in the second line we realize that we can’t have our password hardcoded in the source file like this, so we’ll need to think about some configuration files before we put it in production. Luckily, we are just developing on our local machine now so we won’t worry about it just yet, but this is something we need to remember.

Next, we will install the express module to build an HTTP server for our API. Again, there are a lot of different ways to do it without express — using http, connect, koa, hapi, sails, and many many others. We read about all of them and then let’s say we choose express, for some reason. That’s another “let’s say” to add to the collection, but we have to choose something, so why not express.

This time, express installs cleanly and we can create our server. So we write a simple express app that connects to Mongo and listens for incoming HTTP connections on port 3030:

var mongo = require('mongodb').MongoClient;
var express = require('express');
var dbUrl = 'mongodb://newsreader:secure1@localhost:27017/backendera';
mongo.connect(dbUrl, function (err, db) {
    if (err) throw err;
    var news = db.collection('news');
var app = express();
    app.get('/news', function (req, res) {
news.find({}).toArray(function (err, array) {
if (err) {
} else {

And it works. That was pretty easy. Almost too easy. The more we think about it, the more potential problems we’ll start seeing. We’ll focus on them in the next part.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.