2
votes

I am a newbie in nodejs. I have a basic nodejs app which runs fine on my windows PC with nodejs v10.15.0 and npm v6.9.0 but when I deploy it to Azure App Service on windows platform with node version 10.15.2 , I get 500 internal server error. I have tried debugging using iisnode loggingEnabled flag in web.config and I can see following message being logged. I am not sure if its warning or error which is causing 500.

"(node:25936) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead."

Config :

{
  "name": "redis_test_app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" 
             && exit 1",
    "start": "node index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "applicationinsights": "^1.4.0",
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "livereload": "^0.7.0",
    "redis": "^2.8.0"
  }
}

web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
     This configuration file is required if iisnode is used to run node processes behind
     IIS or IIS Express.  For more information, visit:
     https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
-->

<configuration>
  <system.webServer>  
    <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
    <webSocket enabled="false" />
    <handlers>
      <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
      <add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^index.js\/debug[\/]?" />
        </rule>

        <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
        <rule name="StaticContent">
          <action type="Rewrite" url="public{REQUEST_URI}"/>
        </rule>

        <!-- All other URLs are mapped to the node.js site entry point -->
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="index.js"/>
        </rule>
      </rules>
    </rewrite>

    <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>

    <!-- Make sure error responses are left untouched -->
    <httpErrors existingResponse="PassThrough" />

    <!--
      You can control how Node is hosted within IIS using the following options:
        * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
        * node_env: will be propagated to node as NODE_ENV environment variable
        * debuggingEnabled - controls whether the built-in debugger is enabled
      See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
    -->
    <iisnode flushResponse="true" loggingEnabled="true"  logDirectory="iisnode" watchedFiles="web.config;*.js"/>
  </system.webServer>
</configuration>

app.js

const appInsights = require("applicationinsights");
appInsights.setup("")
.setAutoDependencyCorrelation(true)
    .setAutoCollectRequests(true)
    .setAutoCollectPerformance(true)
    .setAutoCollectExceptions(true)
    .setAutoCollectDependencies(true)
    .setAutoCollectConsole(true)
    .setUseDiskRetryCaching(true)
    .start();

var redis = require('redis');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var client = redis.createClient(xxxx, 'gfkdhggk865487766jggdfgdf', {
    auth_pass: 'passcode',
    tls: { servername: 'xxxxxxxx' }
  });
var config = require('./server.config');
var port = config.port;

// allow cross origin 
app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/', require('./src/routes')());

app.listen(port, function () {
    console.log(`Maintenance server listening on port ${port}!`)
});

// check if the client is connected to redis
client.on('connect', function () {
    console.log('Connected to Redis');
});
2

2 Answers

1
votes

You can enable node.js application level log to see the error message.

The log you pasted here is only a warning, not an error. You can find the new Buffer() methods in your codes and replace them with a new one to avoid this.

Make sure you have changed the listen port when you deploy your project to Azure. You should use app.listen(process.env.PORT); in your app.js file.

Here is a guide regarding deploying nodejs app to azure for your reference.

0
votes

As Caiyi Ju said, you need to listen on the port specified by the process.env.PORT variable. This is a way for Azure to provide a port number to your application.