Build a Backend With Node.js, Container and Azure (1)

These years I have worked with several projects with non-profit organizations and Startups. Here are some common features to these projects.

  • Scalability: it starts from small, but need scale up easily
  • Agility: be able to move to different platforms
  • Flexibility: components and features might be expanded or replaced
  • Management & Operations: not to keep own infrastructure
  • Limited budget

Rails and Heroku are used to be my first choice for these. RoR is an amazing framework with a strong ecosystem. While Heroku offers instant deployment, easy scaling and requires little maintainable.

Until last year, I switched to Node.js and Docker container for even more simplicity and scalability. Lets uses an example to prove it.

Example application

Let’s say we are going to build a REST API for user registration. User sign-up your service with a mobile or web client which will call the API. After a successful signup, the backend will send the user a welcome email with a confirmation link.

It contains the following services:

  • Registration API
  • MongoDB
  • Email-worker
  • Message Queue

setup

install node.js

First we need to install node and updating npm on our dev machine. link

start MongoDB and RabbitMQ services

Login to Microsoft Azure porta
(if you don’t have an Azure Subscription, get a free trial)

Go to “Virtual Machines” and create a vm from Docker on Unbuntu Server image

SSH into your new created VM

then start services with the following command

$ docker run -d --name mongo-server -p 27017:27017 mongo:3
$ docker run -d --name rabbit-server -p 15672:15672 -p 4369:4369 -p 5671:5671 -p 5672:5672 -p 25672:25672 --hostname rabbit  -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin rabbitmq:3-management

run

$ docker ps

you should see containers of MongoDB and RabbitMQ are up and running.

REST API

in next post, we are going to implement a REST API service using Node.js and deploy to it Azure website. Keep tuned.