为什么我发布到 Azure 应用服务的 express Node.js 应用会在浏览器中抛出内部服务器错误?

Why does my express Node.js app published to an Azure App Service throw and internal server error in the browser?

我正在尝试将 Node.js Web 应用程序首次部署到 Azure 应用程序服务。 我正在通过 VS 2019 发布,看起来发布正确。

我可以使用默认项目创建附带的标准 server.js 代码片段进行发布。在浏览器中,我看到正确的 Azure URL 并且“Hello World”显示正确。

这是默认的代码片段:

'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);

我正在尝试使用 express 创建一个简单的 WebService API。

下面是代码片段:

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...');
});

我已经通过 VS 2019 将 express 包添加到项目中,因此它确实显示在我的 npm 包列表和我的 packages.json 文件中:

{
  "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",
  }
}

似乎已成功发布到我的 Azure 应用服务 (Windows)。 但是,当我在浏览器 (Chrome) 中点击 URL 时,我收到以下错误消息:

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

这是一个简单的 Node.js 应用程序,可以通过 VS 2019 和命令行在本地正确运行。 我的代码模块或包定义有问题吗? 我是否缺少正确发布 npm 依赖项的步骤? 我如何追踪这个错误? 我应该使用 Express 模板吗?

在 Azure - Application Performance Analyzer - Failed Requests 中,我看到以下错误信息:

方法请求路径持续时间失败模块故障状态最终状态详细信息 GET / 51.28 秒 iisnode 500.1001 200.0
GET / 51.46 秒 iisnode 500.1001 200.0
请求详细信息 请求http://WebServerTest20220214013438:80/ RequestId8000032f-0000-e700-b63f-84710c7967bbReasonInternal Server Error HTTP Status200.0CS-Bytes1447SC-Bytes452 花费的总时间 51,284 毫秒错误代码 0ModuleNameiisnodeNotificationExecuteRequestHandler

  • 我创建了一个 Express Node.js 应用程序并尝试将其发布到 Azure。

  • 我收到以下错误。

  • 我使用了 Azure Node.js Express 应用程序模板 并添加了您的代码以创建 WebService API,发布到Azure并能够访问URL。

  • 我的package.json长得像
{
 "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"
 }
}
  • The page cannot be displayed because an internal server error has occurred.

  • 在 Azure 门户中,在 KUDU 控制台中检查您的文件是否在 wwwroot 文件夹下。 按照此 link、<your_web_app_name>.azurewebsites.net > Debug Console(从顶部菜单)> CMD/PowerShell > Site > wwwroot。那应该包含你所有的文件。

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
  • 要使此节点应用程序在 Azure(IIS) 上运行,我们需要一个 web.config 文件。
  • 每个应用程序都有默认的根路径 (/) 映射到 D:\home\site\wwwroot,您的代码已部署到此处默认。如果您的应用根目录位于不同的文件夹中,或者您的存储库中有多个应用程序,您可以编辑或添加虚拟应用程序和目录。

更多信息请参考MS Doc , and