loading...

A small gotcha with NodeJS Application settings on Azure that can surprise you

codeprototype profile image Kevin Le ・2 min read

Also posted on Medium

The following code runs perfectly fine on a NodeJS server that’s hosted outside of Azure App Services:

const apiAiApp = require("apiai")(Constants.API_AI_ACCESS_TOKEN);

module.exports = function(app, server) {
   //Do something with apiAiApp
}

During development, running NodeJS on a local server, the environment variable API_AI_ACCESS_TOKEN are set in package.json file. Just for completeness, let me show the constants.js file, since it only takes a few lines:

const env = {
  API_AI_ACCESS_TOKEN: process.env.API_AI_ACCESS_TOKEN,
}

On Azure, the environment variables such as API_AI_ACCESS_TOKEN has to be set in the Application Settings blade. That’s the UI way of doing it. We can also set it using a command line. Either way, the setting of environment variable has to be done first.
But even then, when we try to access the web application, it gives an error page that looks something like

iisnode encountered an error when processing the request.

 HRESULT: 0x6d
 HTTP status: 500
 HTTP subStatus: 1013
 HTTP reason: Internal Server Error

IIS? What is that? If this is the first time you try to deploy your app on Azure, you might not feel so “pressured”. But in my case, my app used to work just fine in production. Suddenly, nothing works anymore on Azure and there’s no console to look at, like on my local server.
Here’s a good resource that helped me out:

https://blogs.msdn.microsoft.com/azureossds/2015/08/19/debug-node-js-web-apps-on-azure/

Long story short, the fix is extremely simple. Simply restructure the code as follow:

const apiAi = require("apiai");

module.exports = function(app, server) {
   const apiAiApp = apiAi(Constants.API_AI_ACCESS_TOKEN);

   //Now, do something with apiAiApp
}

Posted on by:

codeprototype profile

Kevin Le

@codeprototype

Driven by passion and patience

Discussion

markdown guide