将 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.

须安装 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.

如果未看到“基本 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.

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

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

填写字段使新存储库得以发布,然后单击“发布”。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.

单击“创建”以创建应用服务。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.

警告

应用服务部署过程使用一组试探法来确定要尝试和运行哪种类型的应用程序。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.

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

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

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

完成同步后,将出现一个复选标记。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.

部署完成后,导航到公共站点并向 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.

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

  1. python网站模板下载_Python 的 Web 应用程序模板 - Visual Studio | Microsoft Docs

    Python Web 应用程序项目模板Python web application project templates 01/28/2019 本文内容 Visual Studio 中的 Python ...

  2. vs2017附加linux进程,使用调试器附加到运行的进程 - Visual Studio | Microsoft Docs

    使用 Visual Studio 调试器附加到正在运行的进程Attach to running processes with the Visual Studio debugger 06/12/2020 ...

  3. clickonce 部署能cs程序_配置 ClickOnce 信任提示行为 - Visual Studio | Microsoft Docs

    如何:配置 ClickOnce 信任提示行为How to: Configure the ClickOnce trust prompt behavior 11/04/2016 本文内容 您可以配置 Cl ...

  4. node.js 静态属性_如何使用静态站点和Node.js开发和部署第一个全栈式Web应用程序

    node.js 静态属性 This tutorial will show you how to convert a static website that uses HTML, CSS and Jav ...

  5. html+css+js写抽奖程序

    html+css+js写抽奖程序 简介:本文讲解,如何使用html+css+js写抽奖程序,后面也会加上后端,记录每一次的抽奖的结果. HTML结构 这个html结构就十分的简单,几行而已. < ...

  6. linux安装Node.js(详细)Node.js安装教程

    linux安装Node.js(详细)Node.js安装教程 文章目录 linux安装Node.js(详细)Node.js安装教程 1:下载 2:解压 3:移动目录 1:创建目录 2:移动目录并重命名 ...

  7. Linux部署Node.js应用

    Linux部署Node.js应用 文章目录 Linux部署Node.js应用 一.背景描述 二.环境准备 三.添加守护进程 四.参考链接 一.背景描述 将基于Node.js开发的应用部署到Linux系 ...

  8. Linux 创建网页服务,Linux使用Node.js建立访问静态网页的服务实例详解

    Linux使用Node.js建立访问静态网页的服务实例详解 一.安装node.js运行所需要的环境,: 二.创建node目录(/node/www),并在目录下创建node.js服务文件server.j ...

  9. linux node 命令无效,完美解决linux下node.js全局模块找不到的情况

    今天在在linux上用npm安装了pm2准备部署node项目,结果通过pm2命令启动项目的时候报pm2找不到,这很伤,以为pm2没有安装成功,但是在node安装目录下面的bin文件夹里面调用pm2却没 ...

最新文章

  1. AAAI 2019 论文解读 | 基于区域分解集成的目标检测
  2. .net remoting 技术
  3. apache 配置php 版本错误u,PHP+Apache+MySQL 在配置环境时遇到的问题
  4. 2018年小米竟发布了16款手机,然而最值得入手的是它!
  5. ios java环境变量_iOS 环境变量配置(DebugReleaseTest)
  6. javascirpt IP验证
  7. php随机数怎么获取?一个简单的函数就能生成
  8. The content of element type web-app must match……等 问题之解决办法
  9. 【Microsoft Azure 的1024种玩法】五十五.Azure speech service之通过JavaScript快速实现文本转换为语音
  10. arduino ps2摇杆程序_Arduino 双轴按键摇杆 PS2游戏摇杆 控制杆 JoyStick 电子积木
  11. 关于多个Jenkins实例共享同一个工作目录的问题
  12. 阿里云邮箱25端口被关闭,改用465端口Java发送邮件
  13. 【初识AI】(一):ASR和NLP
  14. 【SolidWorks】模型状态显示设置介绍
  15. Js判断是否为非数字
  16. Android关机闹钟实现
  17. 树莓派开发笔记(十一):蓝牙的使用,BlueZ协议(双树莓探测rssi并通过蓝牙互传获取的rssi信号强度)
  18. 修改浏览器User-Agent
  19. 【LAMP 基于 Red Hat Linux 7】
  20. 关于注册Github不能通过验证的解决方法(easy,一定过)

热门文章

  1. Eclipse里PHP built-in server在操作系统中的实际位置
  2. 隐藏SAP CRM WebClient UI personalization 里某个field
  3. No authorization to log on as a Trusted System
  4. Currency determination callback
  5. Use function as controller
  6. Kubernetes kubectl proxy命令的妙用
  7. 大数据的说法 正确的是_前端测试题:(解析)用于播放音频文件的正确HTML5元素是?...
  8. datetimepicker弹出窗在iframe中部分遮挡_美军下一代班组武器项目,NGSW中的小秘密,你了解多少?...
  9. python random 和numpy random_Python中numpy.random和random.random之间的区别
  10. postgresql参数化查询_一个能融会贯通PostgreSQL监控的人,大概率是高手