NodeJs Microservices on Azure

Note: A Github template repo for setting these up can be found at JediMindtrick/NodeMicroservice

Getting a NodeJs microservice up and running on Azure is surprisingly simple. This won’t be a full walkthrough, but instead a recipe on how to accomplish this. I am assuming you know at least something about the three big words in the title: NodeJs, Microservices and Azure. You should also have a Github account, NodeJs installed and an Azure account.

  1. Setup an Azure website that will be auto-deployed from your github repository. You can follow the instructions here. (don’t worry about the “mac” part, this will work for any OS).
  2. Let’s get a little more sophisticated than that. Use Express to create some RESTful routes. After you are finished, push to git and check it out via your public azure URL.
  3. You need to secure your microservice against bad-guys. Https + basic auth is pretty standard and should do the trick for most scenarios. Let’s start with only accepting requests over https. For this basic recipe, we’re going to use Azure’s cert. Add this web.config to your repository
  4. Add a rule to it telling IIS (on Azure websites NodeJs runs under IIS) to redirect all non-http calls to the equivalent https endpoint.
  5. Commit your changes to your repository. Check it out up on your public azure url (obtained in #1) and make sure your calls to normal http urls are getting properly redirected to https urls.
  6. Now that our transport is secure, add some basic auth to your express application.
  7. Storing credentials in code is a bad, bad, bad idea. Following the tenets of the 12-factor app, let’s use environment variables instead. You read the environment variable in NodeJs using “process.env.VARIABLE_NAME_HERE” Use this to obtain your username and password for your express basic auth.
  8. Setting environment variables on your local machine depends on your OS. Setting them on an Azure website is done by setting them in your website -> Configure tab -> app settings.
  9. Deploy your changes again. Now if doing a GET request, for example, you should be redirected to an https url and prompted for credentials. Any consuming client will need to supply the credentials you have set in #7 & #8 to your microservice.

Although this recipe is broken down into 9 steps, conceptually it’s really just Azure+NodeJs+BasicAuth. From a time perspective I was able to find all the missing pieces I had and put this together in just a few hours of work. You can find an example service I wrote, “Config Server”, up on Github.