js写的程序如何上线到linux,将 Node.js 应用发布到 Linux 应用服务 - Visual Studio | Microsoft Docs...

将 Node.js 应用程序发布到 Azure(Linux 应用服务)Publish a Node.js application to Azure (Linux App Service)

11/22/2019

本文内容

本教程指导你完成创建简单 Node.js 应用程序并将其发布到 Azure 的任务。This tutorial walks you through the task of creating a simple Node.js application and publishing it to Azure.

将 Node.js 应用程序发布到 Azure 时,提供几个选项。When publishing a Node.js application to Azure, there are several options. 其中包括 Azure 应用服务、运行所选 OS 的 VM、用于 Kubernetes 管理的 Azure 容器服务 (AKS)、使用 Docker 的容器实例等等。These include Azure App Service, a VM running an OS of your choosing, Azure Container Service (AKS) for management with Kubernetes, a Container Instance using Docker, and more. 有关各选项的详细信息,请参阅计算。For more details on each of these options, see Compute.

对于本教程,将应用部署到 Linux 应用服务。For this tutorial, you deploy the app to Linux App Service.

Linux 应用服务部署 Linux Docker 容器来运行 Node.js 应用程序(不同于 Windows 应用服务,后者在 Windows 上 IIS 后运行 Node.js 应用程序)。Linux App Service deploys a Linux Docker container to run the Node.js application (as opposed to the Windows App Service, which runs Node.js apps behind IIS on Windows).

本教程从随针对 Visual Studio 的 Node.js 工具安装的一个模板开始,演示如何创建 Node.js 应用程序,并将代码推动到 GitHub 上的存储库,然后通过 Azure Web 门户预配 Azure 应用服务,以便可以从 GitHub 存储库进行部署。This tutorial shows how to create a Node.js application starting from a template installed with the Node.js Tools for Visual Studio, push the code to a repository on GitHub, and then provision an Azure App Service via the Azure web portal so that you can deploy from the GitHub repository. 若要使用命令行来预配 Azure 应用服务并从本地 Git 存储库推送代码,请参阅创建 Node.js 应用。To use the command-line to provision the Azure App Service and push the code from a local Git repository, see Create Node.js App.

在本教程中,你将了解:In this tutorial, you learn how to:

创建 Node.js 项目Create a Node.js project

为代码创建 GitHub 存储库Create a GitHub repository for the code

创建 Azure 上的 Linux 应用服务Create a Linux App Service on Azure

部署到 LinuxDeploy to Linux

先决条件Prerequisites

须安装 Visual Studio 且具有 Node.js 开发工作负载。You must have Visual Studio installed and the Node.js development workload.

如果尚未安装 Visual Studio 2019,请转到 Visual Studio 下载页免费安装。If you haven't already installed Visual Studio 2019, go to the Visual Studio downloads page to install it for free.

如果尚未安装 Visual Studio 2017,请转到 Visual Studio 下载页免费安装。If you haven't already installed Visual Studio 2017, go to the Visual Studio downloads page to install it for free.

如果需要安装工作负载但已有 Visual Studio,请转到“工具” > “获取工具和功能...”,这会打开 Visual Studio 安装程序。If you need to install the workload but already have Visual Studio, go to Tools > Get Tools and Features..., which opens the Visual Studio Installer. 选择“Node.js 开发”工作负载,然后选择“修改” 。Choose the Node.js development workload, then choose Modify.

5e039389015c14c35626e959782b736f.png

须安装 Node.js 运行时。You must have the Node.js runtime installed.

如果未安装,请从 Node.js 网站安装 LTS 版本。If you don't have it installed, install the LTS version from the Node.js website. 一般情况下,Visual Studio 会自动检测已安装的 Node.js 运行时。In general, Visual Studio automatically detects the installed Node.js runtime. 如果系统未检测到已安装运行时,则可以将项目配置为引用属性页中已安装的运行时(创建项目后,右键单击项目节点并选择“属性”)。If it does not detect an installed runtime, you can configure your project to reference the installed runtime in the properties page (after you create a project, right-click the project node and choose Properties).

创建要在 Azure 中运行的 Node.js 项目Create a Node.js project to run in Azure

打开 Visual Studio。Open Visual Studio.

创建新的 TypeScript Express 应用。Create a new TypeScript Express app.

按 Esc 关闭启动窗口。Press Esc to close the start window. 键入 Ctrl+Q 以打开搜索框,键入“Node.js”,然后选择“创建新的基本 Azure Node.js Express 4 应用程序”(TypeScript) 。Type Ctrl + Q to open the search box, type Node.js, then choose Create new Basic Azure Node.js Express 4 application (TypeScript). 在出现的对话框中,选择“创建”。In the dialog box that appears, choose Create.

从顶部菜单栏中选择“文件” > “新建” > “项目”。From the top menu bar, choose File > New > Project. 在“新建项目”对话框的左窗格中,展开“TypeScript”,然后选择“Node.js” 。In the left pane of the New Project dialog box, expand TypeScript, then choose Node.js. 在中间窗格中,选择“基本 Azure Node.js Express 4 应用程序”,然后选择“确定” 。In the middle pane, choose Basic Azure Node.js Express 4 application, then choose OK.

3be73250eb96c4fd5861d6f1e90ac0c2.png

如果未看到“基本 Azure Node.js Express 4 应用程序”项目模板,必须添加 Node.js 开发工作负载 。If you don't see the Basic Azure Node.js Express 4 application project template, you must add the Node.js development workload. 有关详细说明,请参阅先决条件。For detailed instructions, see the Prerequisites.

Visual Studio 将创建项目并在解决方案资源管理器(右窗格)中将其打开。Visual Studio creates the project and opens it in Solution Explorer (right pane).

按 F5 来生成并运行应用,确保一切按预期运行。Press F5 to build and run the app, and make sure that everything is running as expected.

选择“文件” > “添加到源代码管理”,创建项目的本地 Git 存储库。Select File > Add to source control to create a local Git repository for the project.

此时,采用 Express 框架且以 TypeScript 编写的 Node.js 应用正常工作并签入到本地源代码管理。At this point, a Node.js app using the Express framework and written in TypeScript is working and checked in to local source control.

根据需要编辑项目,再继续到下一步骤。Edit the project as desired before proceeding to the next steps.

将代码从 Visual Studio 推送到 GitHubPush code from Visual Studio to GitHub

设置用于 Visual Studio 的 GitHub:To set up GitHub for Visual Studio:

Make sure the GitHub Extension for Visual Studio is installed and enabled using the menu item Tools > Extensions and Updates.

从菜单中选择“视图” > “其他窗口” > “GitHub”。From the menu select View > Other Windows > GitHub.

GitHub 窗口随即打开。The GitHub window opens.

如果在 GitHub 窗口中没有看到“开始操作”按钮,请单击“文件” > “添加到源代码管理”并等待 UI 更新。If you don't see the Get Started button in the GitHub window, click File > Add to Source Control and wait for the UI to update.

a2e28daa05abe185af437cd940bb6e01.png

单击“开始操作”。Click Get started.

如果已连接到 GitHub,工具箱将显示,类似于下图。If you are already connected to GitHub, the toolbox appears similar to the following illustration.

2f8a9eeacf4041deae12369335dd72e3.png

填写字段使新存储库得以发布,然后单击“发布”。Complete the fields for the new repository to publish, and then click Publish.

几分钟后会显示一个横幅,指出“已成功创建存储库”。After a few moments, a banner stating "Repository created successfully" appears.

下一步部分中,将学习如何从此存储库发布到 Linux 上 Azure 应用服务。In the next section, you learn how to publish from this repository to an Azure App Service on Linux.

创建 Azure 中的 Linux 应用服务Create a Linux App Service in Azure

从左侧服务列表中选择“应用服务”,然后单击“添加”。Select App Services from the list of services on the left, and then click Add.

如果要求,请创建新的资源组和应用服务计划来托管新应用。If required, create a new Resource Group and App Service plan to host the new app.

确保将“OS”设为“Linux”,将“运行时堆栈”设为所需 Node.js 版本,如图所示 。Make sure to set the OS to Linux, and set Runtime Stack to the required Node.js version, as shown in the illustration.

29bd49493b87c807567710eb13ba3b0a.png

单击“创建”以创建应用服务。Click Create to create the App Service.

部署可能需要几分钟的时间。It may take a few minutes to deploy.

部署后,请转到“应用程序设置”部分,添加一个名为 SCM_SCRIPT_GENERATOR_ARGS、值为 --node 的设置。After it is deployed, go to the Application settings section, and add a setting with a name of SCM_SCRIPT_GENERATOR_ARGS and a value of --node.

f1ecccc95178e3cd04683579242cbf50.png

警告

应用服务部署过程使用一组试探法来确定要尝试和运行哪种类型的应用程序。The App Service deployment process uses a set of heuristics to determine which type of application to try and run. 如果在已部署的内容中检测到 .sln 文件,则假设正在部署基于 MSBuild 的项目。If a .sln file is detected in the deployed content, it will assume an MSBuild based project is being deployed. 上面添加的设置将覆盖此逻辑,并显式指定这是 Node.js 应用程序。The setting added above overrides this logic and specifies explicitly that this is a Node.js application. 如果没有此设置,当 .sln 文件属于正部署到应用服务的存储库时,Node.js 应用程序将部署失败。Without this setting, the Node.js application will fail to deploy if the .sln file is part of the repository being deployed to the App Service.

在“应用程序设置”下,添加另一个名称为 WEBSITE_NODE_DEFAULT_VERSION 且值为 8.9.0 的设置。Under Application settings, add another setting with a name of WEBSITE_NODE_DEFAULT_VERSION and a value of 8.9.0.

部署后,打开应用服务,并选择“部署选项”。After it is deployed, open the App Service and select Deployment options.

9edaa025c7c561b1b90834b8c265a799.png

单击“选择源”,选择“GitHub”,然后配置任何所需的权限。Click Choose source, and then choose GitHub, and then configure any required permissions.

2bae2fa1e372d6341e70b0b6f4b7f057.png

选择要发布的存储库和分支,并选择“确定”。Select the repository and branch to publish, and then select OK.

d92421b847194e93fa4eae6d85395bec.png

同步时“部署选项”页将出现。The deployment options page appears while syncing.

e808f3ffea593e8de5b7d2b8c2958c48.png

完成同步后,将出现一个复选标记。Once it is finished syncing, a check mark will appear.

站点现在运行来自 GitHub 存储库的 Node.js 应用程序,可通过为 Azure 应用服务创建的 URL 进行访问(默认情况下为 Azure 应用服务提供的名称后跟“.azurewebsites.net”)。The site is now running the Node.js application from the GitHub repository, and it is accessible at the URL created for the Azure App Service (by default the name given to the Azure App Service followed by ".azurewebsites.net").

修改应用并推送更改Modify your app and push changes

在 app.use('/users', users); 行之后,将此处显示的代码添加到 app.ts。Add the code shown here in app.ts after the line app.use('/users', users);. 这会在 URL /api 处添加 REST API。This adds a REST API at the URL /api.

app.use('/api', (req, res, next) => {

res.json({"result": "success"});

});

生成代码并进行本地测试,然后将其签入并推送到 GitHub。Build the code and test it locally, then check it in and push to GitHub.

在 Azure 门户中,需要一些时间来检测 GitHub 存储库中的更改,然后开始再次同步部署。In the Azure portal, it takes a few moments to detect changes in the GitHub repo, and then a new sync of the deployment starts. 它看上去类似于下图。This looks similar to the following illustration.

3a37b9ee4c1dc4f3a05e1b5d0999d544.png

部署完成后,导航到公共站点并向 URL 追加 /api。Once deployment is complete, navigate to the public site and append /api to the URL. 返回 JSON 响应。The JSON response gets returned.

疑难解答Troubleshooting

如果 node.exe 进程发生故障(也就是说出现未处理的异常),容器将重新启动。If the node.exe process dies (that is, an unhandled exception occurs), the container restarts.

容器启动时,它通过各种试探法来找出如何启动 Node.js 进程。When the container starts up, it runs through various heuristics to figure out how to start the Node.js process. Details of the implementation can be seen at generateStartupCommand.js.

可以通过 SSH 连接到正在运行的容器进行调查。You can connect to the running container via SSH for investigations. 这可以通过 Azure 门户轻松实现。This is easily done using the Azure portal. 选择应用服务,向下滚动工具列表直至“开发工具”部分下的“SSH” 。Select the App Service, and scroll down the list of tools until reaching SSH under the Development Tools section.

若要帮助进行故障排除,请转到应用服务的“诊断日志”设置,并将“Docker 容器日志记录”设置从“关”改为“文件系统” 。To aid in troubleshooting, go to the Diagnostics logs settings for the App Service, and change the Docker Container logging setting from Off to File System. 日志创建在 /home/LogFiles/_docker.log* 中,并且可以通过 SSH 或 FTP 在框上进行访问。Logs are created in the container under /home/LogFiles/ _docker.log*, and can be accessed on the box using SSH or FTP(S).

可向站点分配自定义域名,而不是默认分配的 *.azurewebsites.net URL。A custom domain name may be assigned to the site, rather than the *.azurewebsites.net URL assigned by default. 有关更多详细信息,请参阅主题映射自定义域。For more details, see the topic Map Custom Domain.

最好先部署到过渡站点进行进一步测试,再移动到生产环节。Deploying to a staging site for further testing before moving into production is a best practice. 有关如何配置此项的详细信息,请参阅主题创建过渡环境。For details on how to configure this, see the topic Create staging environments.

See the App Service on Linux FAQ for more commonly asked questions.

后续步骤Next steps

本教程中,你学习了如何创建 Linux 应用服务并将 Node.js 应用程序部署到该服务。In this tutorial, you learned how create a Linux App Service and deploy a Node.js application to the service. 你可能想要详细了解 Linux 应用服务。You may want to learn more about Linux App Service.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值