Forem

Kevin Le
Kevin Le

Posted on

2 1

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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (1)

Collapse
 
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?

Thx
Jay

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay