Home > Blockchain >  Why does my express Node.js app published to an Azure App Service throw and internal server error in
Why does my express Node.js app published to an Azure App Service throw and internal server error in

Time:02-16

I am attempting my first deployment of a Node.js web app to an Azure App Service. I am publishing through VS 2019 and it looks like it's publishing correctly.

I am able publish using the standard server.js code snippet that comes with default project creation. In a browser I see the correct Azure URL and "Hello World" displays correctly.

Here is the default code snippet:

'use strict';
var http = require('http');
var port = process.env.PORT || 1337;

http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');
}).listen(port);

I'm trying to create a simple WebService API using express.

Here is the code snippet for that:

const express = require('express');
const server = express();

server.get('/', (req, res) => {
    res.send('root:: Azure Express Web Server Successful!');
});


server.listen(4242, () => {
    console.log('Express Test is Running...');
});

I've added the express package through VS 2019 to the project so it does show up in my list of npm packages and in my packages.json file:

{
  "name": "foo-bar",
  "version": "0.0.0",
  "description": "FooBar",
  "scripts": {
    "start": "node server"
  },
  "engines": {
    "node": "~6.10.x"
  },
  "author": {
    "name": ""
  },
  "dependencies": {
    "express": "^4.17.2",
  }
}

It appears to publish successfully to my Azure App Service (Windows). However when I hit the URL in a browser (Chrome), I get the following error message:

The page cannot be displayed because an internal server error has occurred.

This is a simple Node.js app that runs correctly locally thru VS 2019 and command line. Is there something wrong with my code module or package definition? Am I missing a step for correctly publish the npm dependency? How do I trace this error down? Should I be using the Express template?

From Azure - Application Performance Analyzer - Failed Requests, I see the following error information:

Method Request Path Duration Failing Module Failure Status Final Status Details GET / 51.28 sec iisnode 500.1001 200.0
GET / 51.46 sec iisnode 500.1001 200.0
Request Details Requesthttp://WebServerTest20220214013438:80/ RequestId8000032f-0000-e700-b63f-84710c7967bbReasonInternal Server Error HTTP Status200.0CS-Bytes1447SC-Bytes452 Total Time Spent51,284 msError Code0ModuleNameiisnodeNotificationExecuteRequestHandler

CodePudding user response:

  • I have created a Express Node.js App and tried to publish it to Azure.

  • I got the below error. enter image description here

  • I have taken the Azure Node.js Express Application Template and added your code to create WebService API, Published to Azure and able to access the URL. enter image description here

enter image description here

  • My package.json looks like
{
 "name": "express-app4",
 "version": "0.0.0",
 "private": true,
 "scripts": {
   "build": "tsc --build",
   "clean": "tsc --build --clean",
   "start": "node app"
 },
 "description": "ExpressApp4",
 "author": {
   "name": ""
 },
 "dependencies": {
   "debug": "^2.2.0",
   "express": "^4.17.2",
   "pug": "^2.0.0-rc.3"
 },
 "devDependencies": {
   "@types/debug": "0.0.30",
   "@types/express": "^4.0.37",
   "@types/express-serve-static-core": "^4.0.50",
   "@types/mime": "^1.3.1",
   "@types/serve-static": "^1.7.32",
   "@types/node": "^14.14.7",
   "typescript": "^4.0.5"
 },
 "engines": {
   "node": "~6.10.x"
 }
}
Method Request Path Duration Failing Module Failure Status Final Status Details GET / 51.28 sec iisnode 500.1001 200.0  
GET / 51.46 sec iisnode 500.1001 200.0  
Request Details Requesthttp://WebServerTest20220214013438:80/ RequestId8000032f-0000-e700-b63f-84710c7967bbReasonInternal Server Error HTTP Status200.0CS-Bytes1447SC-Bytes452 Total Time Spent51,284 msError Code0ModuleNameiisnodeNotificationExecuteRequestHandler
  • To make this node app work on Azure(IIS), we need a web.config file.
  • Each app has the default root path (/) mapped to D:\home\site\wwwroot, where your code is deployed by default. If your app root is in a different folder, or if your repository has more than one application, you can edit or add virtual applications and directories.

Please refer MS Doc , Link1 and Link2 for more information.

  • Related