【问题标题】:Why does my express Node.js app published to an Azure App Service throw and internal server error in the browser?为什么我的快速 Node.js 应用程序发布到 Azure 应用程序服务会在浏览器中引发内部服务器错误?
【发布时间】:2022-02-15 15:05:10
【问题描述】:

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

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

这里是默认代码sn-p:

'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。

这里是sn-p的代码:

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 时,我收到以下错误消息:

页面无法显示,因为内部服务器错误 发生了。

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

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

方法请求路径持续时间失败模块失败状态最终状态详细信息 获取 / 51.28 秒 iisnode 500.1001 200.0
获取 / 51.46 秒 iisnode 500.1001 200.0
请求详细信息 请求http://WebServerTest20220214013438:80/ RequestId8000032f-0000-e700-b63f-84710c7967bbReasonInternal 服务器错误 HTTP状态200.0CS-Bytes1447SC-Bytes452 Total Time Spent51,284 msError Code0ModuleNameiisnodeNotificationExecuteRequestHandler

【问题讨论】:

  • 在 azure 仪表板中肯定有一个日志查看器可以告诉你发生了什么。
  • 刚接触 Azure,试图弄清楚这一点。查看了此服务的活动日志,但没有看到任何内容。当然,这肯定是在 Azure 和本地配置/使用 express 之间的区别。 Node.js 新手,但在源 sn-p 或 packages.json 文件中看不到任何明显内容。

标签: node.js express azure-web-app-service azure-appservice


【解决方案1】:
  • 我创建了一个 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"
 }
}
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 DocLink1Link2了解更多信息。

【讨论】:

  • 非常感谢!使用 Express 4 模板(正如您在示例中所做的那样),我能够立即使其工作而无需更改。使用“Azure Node.js Web 应用程序”会导致问题。
  • 通过为我的 WebService API 调用添加路由,我能够添加路由。再次感谢!
  • @JohnB - 很高兴它有帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 2016-08-17
  • 1970-01-01
  • 2012-07-29
  • 2016-02-03
相关资源
最近更新 更多