天天看点

【Azure 应用服务】本地Node.js部署上云(Azure App Service for Linux)遇到的三个问题解决之道

问题描述

当本地Node.js(Linux + Node.js + npm + yarn)部署上云,选择 Azure App Service for Linux 环境。但是在部署时,遇见了以下三个问题:

问题一:使用VS Code进行部署,部署速度非常的慢,且长时间无法部署成功

问题二:本地项目文件部署到App Service的WWWROOT目录下,但是项目中的node_modules为空,即App Service没有完成自动化部署

问题三:部署成功后,项目无法访问,端口3000没有被App Service监听

问题解答

问题一:使用VS Code进行部署,部署速度非常的慢,且长时间无法部署成功

因为Node.js 应用必须与所有必需的 NPM 依赖项一起部署。但如果项目根目录文件中包含了 yarn.lock 或者是在 package.json 中指定了 yarn 作为Package Manager,则会使用 yarn 来代替 npm。 且默认会使用最新版的 yarn。这会导致在部署时出现未知问题。如卡顿导致长时间无响应。

【Azure 应用服务】本地Node.js部署上云(Azure App Service for Linux)遇到的三个问题解决之道

解决办法为,删除 yarn.lock 文件。

引用文档:​​https://github.com/microsoft/Oryx/blob/main/doc/runtimes/nodejs.md#package-manager​​

Package manager

The version of npm used to install dependencies and run npm scripts is the one bundled with the specified Node.js version as listed ​​here​​.

If a ​

​yarn.lock​

​​ file is found in your repo root or if you specify "yarn" in the ​

​engines​

​ field of package.json, the latest or specified version of yarn will be used instead of npm.

Note that installing packages globally is unsupported, whether requested directly by your app or by some pre/post install script of an included package. For example, this will not work in your ​

​package.json​

​:

"scripts" : {

"preinstall" : "npm install -g somepackage"

}

问题二:本地项目文件部署到App Service的WWWROOT目录下,但是项目中的node_modules为空,即App Service没有完成自动化部署

因为项目文件已经部署到 wwwroot目录下,但是 node_modules 为空,表示部署时没有完成 npm install 自动编译的命令。所以经过查看关于App Service对部署是否自动build的资料发现,当对App Service进行部署的时候,App Service Kudu会自动执行编译命令。其主要是通过 ​

​SCM_DO_BUILD_DURING_DEPLOYMENT 参数进行控制。​

当 SCM_DO_BUILD_DURING_DEPLOYMENT 设置为 true 时,Kudu会自动编译项目文件,如运行 npm install 或 dotnet build / dotnet pulish 

当 SCM_DO_BUILD_DURING_DEPLOYMENT 设置为false时,则不会。当使用ZIP 包部署时,就不会触发自动编译。因为Kudu认为ZIP中的文件已经是可执行文件,不在需要编译。

Reference : ​​https://github.com/projectkudu/kudu/wiki/Configurable-settings#enabledisable-build-actions​​

解决办法为,在App Service的Configuration中添加 SCM_DO_BUILD_DURING_DEPLOYMENT 参数,并配置值为 true。或者是在项目跟目录中添加 .deployment 文件。并在其中加入 

[config]
SCM_DO_BUILD_DURING_DEPLOYMENT=true      

如图:

【Azure 应用服务】本地Node.js部署上云(Azure App Service for Linux)遇到的三个问题解决之道

问题三:部署成功后,项目无法访问,端口3000没有被App Service监听

Node.js 应用需要侦听正确的端口才能接收传入的请求

当部署完成,但无法通过浏览器访问App Service 。这时候,就可以直接登录Kudu 查看 docker 日志。在日志中看见 app service默认监听的端口为8080。

【Azure 应用服务】本地Node.js部署上云(Azure App Service for Linux)遇到的三个问题解决之道

而 Node.js 项目中,使用的确是3000,所以需要通过配置App Service的 Application Setting来解决这个问题。

解决办法为:通过App Service门户,添加 PORT 或者 ​

​WEBSITES_PORT​

​ 应用配置参数值为3000.

Reference:

​​https://docs.azure.cn/zh-cn/app-service/configure-custom-container?pivots=container-linux#configure-port-number​​

​​https://docs.azure.cn/zh-cn/app-service/configure-language-nodejs?pivots=platform-linux#get-port-number​​

继续阅读