如何将 NodeJs 应用程序部署到 Azure Web 应用程序

How to deploy a NodeJs app to azure web app

我接到了将 nodejs 应用程序部署到 azure web 应用程序的任务,但我无法让它工作。

我按照 link 中的说明进行操作,一切似乎都非常简单。

唯一的区别是我的 package.json 启动脚本如下所示。

"scripts": {
   "start": "node ./node_modules/@samplescope/samplefolder/sample_service.js"
  },

在指令 link 的示例源代码中,它有一个 web.config 指向 server.js,所以我将该部分修改为我的启动文件的位置是 "./node_modules/@samplescope/samplefolder/sample_service.js"

由此

    <rule name="DynamicContent">
         <match url="/*" />
         <action type="Rewrite" url="server.js"/>
    </rule>

像这样

    <rule name="DynamicContent">
         <match url="/*" />
         <action type="Rewrite" url="./node_modules/@samplescope/samplefolder/sample_service.js"/>
    </rule>

我也尝试删除我创建的 web.config 因为根据这个 link kudu 会尝试根据你的 package.json 为你生成一个,但它仍然没有用.

我知道我做错了什么,有人能指出我正确的方向吗?

此外,如何在 Azure Web 应用程序中调用 "npm install"?

是的,它在被 "npm start"

调用时在本地工作
一旦在推送请求中检测到 package.json 文件,Azure Web 应用程序就会调用

"npm install"。它会安装 "dependencies" 中列出的软件包。

然而,它并不完美。我的一个项目需要 SQLite,但没有安装。我通过在本地安装所有包然后使用本地 Git

将整个 "node_modules" 文件夹推送到 Azure 来解决这个问题

我不建议将 "sample_service.js" 放入 "node_modules" 中,因为此文件夹是 npm 根据 "package.json" 中列出的依赖项创建和修改的。尝试将 "sample_services.js" 放在 "node_modules" 之外但位于同一文件夹中。

如果 "sample_services.js" 是后端 Javascript 服务器,由于 naming restrictions 当部署。在 "package.json" 中将其指定为 "main" 以便 Azure 将其作为 Windows 中 "node server.js" 的等价物运行 Powershell 使您的后端运行并在 "server.js":

const port = process.env.PORT || 3000; 

生成的 "package.json" 文件应该看起来像这样:

{
  "name": "myProject",
  "version": "1.0.0",
  "engines": {
    "node": "8.10.0",
    "npm": "5.6.0"
  },
  "description": "Project X",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "me",
  "license": "ISC",
  "dependencies": {
    "express": "4.16.3",
    "sqlite3": "4.0.2"
  }
}

Azure 会自动选择要使用的 Node.js 和 npm 版本。如果您有兼容性问题,请像我一样指定 Node.js 和 npm 的版本

此外,如果您像我在之前的项目中那样使用 Express 进行路由,"server.js" 中的这段代码将运行以加载默认的 onload HTML 页面:

app.get('/', function(req,res){
    console.log('default html loading');
    res.render('./public/index.html');
});

同样,由于相同的命名限制,我将 HTML 主页面命名为 index.html

如果您想试用完整示例或比较代码,我发现本指南很有帮助:https://www.codeproject.com/Articles/1133660/Deploy-Node-js-in-Microsoft-Azure

干杯!

我能够解决这个问题,现在能够成功地将我的 nodejs 应用程序部署到 azure web 应用程序。

几个关键点帮助我解决了这个问题,因为我知道 kudu 部署,所以我不太熟悉它。

  1. 您可以通过以下方式获取 kudu 日志文件 https://{site}.scm.azurewebsites.net/ > 工具选项卡 > 诊断转储

  2. 在下面站点的 "Overall Process" 部分下,您将看到 kudu 进程在运行。这个 link 也帮助我理解了 kudu 过程。 https://blog.lifeishao.com/2017/03/24/custom-nodejs-deployment-on-azure-web-app/

  3. 在同步应用程序、清理文件并将文件复制到目标文件夹后,kudu 将尝试生成 web.config。 在我们的例子中,.js 位于私有节点模块之一,因此它需要在 kudu 生成 web.config.

  4. 之前存在
  5. 还建议创建您自己的 deploy.cmd,以便您能够控制部署步骤。您也可以参考此 link 了解更多信息。 https://github.com/projectkudu/kudu/wiki/Custom-Deployment-Script#custom-deployment-script-generator

  6. 如果您要部署带有私有模块的应用程序,您可以参考此 link 作为您的指南。 https://www.jeff.wilcox.name/2017/02/azure-private-npm/ 另外,即使您的本地文件夹中已经有 .npmrc,在执行 npm install 时也要明确指出注册表。

  7. 最后,如果您在 Azure Devops 中使用 Azure 服务应用部署进行部署,请不要让它为您生成 web.config,而是让 kudu 生成它。