DEV Community

Kevin Le
Kevin Le

Posted on

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

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 = {

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:

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

Top comments (1)

jjayaraman profile image
Jayakumar Jayaraman

Thanks, is it safe to check this in public repo if we configure the secrets in the Function App > Application settings and refer the variable in the NodeJS code using process.env.API_AI_ACCESS_TOKEN

IF any one else access the public repo and run the code will it access my Azure resources and add costs?