生成 Node.js 应用程序并将其部署到 Azure 云服务Build and deploy a Node.js application to an Azure Cloud Service

本教程演示如何创建在 Azure 云服务中运行的简单 Node.js 应用程序。This tutorial shows how to create a simple Node.js application running in an Azure Cloud Service. 云服务是 Azure 中可缩放云应用程序的构建基块。Cloud Services are the building blocks of scalable cloud applications in Azure. 可分隔并独立管理云服务,并可扩展应用程序的前端和后端组件。They allow the separation and independent management and scale-out of front-end and back-end components of your application. 云服务为可靠托管每个角色提供强大的专用虚拟机。Cloud Services provide a robust dedicated virtual machine for hosting each role reliably.

有关云服务以及如何将它们与 Azure 网站和虚拟机进行比较的更多信息,请参阅 Azure 网站、云服务和虚拟机的比较For more information on Cloud Services, and how they compare to Azure Websites and Virtual machines, see Azure Websites, Cloud Services and Virtual Machines comparison.

Tip

想要构建简单网站?Looking to build a simple website? 如果方案只涉及一个简单的网站前端,则考虑使用轻型 Web 应用If your scenario involves just a simple website front-end, consider using a lightweight web app. 可随 Web 应用的增长和需求的变化轻松升级到云服务。You can easily upgrade to a Cloud Service as your web app grows and your requirements change.

按照本教程,生成托管在 Web 角色中的简单 Web 应用程序。By following this tutorial, you will build a simple web application hosted inside a web role. 使用计算模拟器在本地测试应用程序,并使用 PowerShell 命令行工具部署该应用程序。You will use the compute emulator to test your application locally, then deploy it using PowerShell command-line tools.

该应用程序为简单的“hello world”应用程序:The application is a simple "hello world" application:

Web 浏览器中显示“Hello World”网页

先决条件Prerequisites

Note

本教程使用 Azure PowerShell,因此需要在 Windows 上运行。This tutorial uses Azure PowerShell, which requires Windows.

创建 Azure 云服务项目Create an Azure Cloud Service project

执行以下任务可创建一个新的 Azure 云服务项目以及基本的 Node.js 基架:Perform the following tasks to create a new Azure Cloud Service project, along with basic Node.js scaffolding:

  1. 以管理员身份运行 Windows PowerShell;在“开始”菜单或“开始”屏幕中,搜索 Windows PowerShell。Run Windows PowerShell as Administrator; from the Start Menu or Start Screen, search for Windows PowerShell.

  2. 连接 PowerShell到订阅。Connect PowerShell to your subscription.

  3. 输入以下 PowerShell cmdlet 来创建项目:Enter the following PowerShell cmdlet to create to create the project:

    New-AzureServiceProject helloworld
    

    New-AzureService helloworld 命令的结果

    New-AzureServiceProject cmdlet 会生成一个基本结构用于将 Node.js 应用程序发布到云服务。The New-AzureServiceProject cmdlet generates a basic structure for publishing a Node.js application to a Cloud Service. 该结构包含向 Azure 发布应用程序所需的配置文件。It contains configuration files necessary for publishing to Azure. 该 cmdlet 还会将工作目录更改为服务的目录。The cmdlet also changes your working directory to the directory for the service.

    该 cmdlet 会创建以下文件:The cmdlet creates the following files:

    • ServiceConfiguration.Cloud.cscfgServiceConfiguration.Local.cscfgServiceDefinition.csdef:发布应用程序时必需的 Azure 特定文件。ServiceConfiguration.Cloud.cscfg, ServiceConfiguration.Local.cscfg and ServiceDefinition.csdef: Azure-specific files necessary for publishing your application. 有关详细信息,请参阅 创建 Azure 托管服务概述For more information, see Overview of Creating a Hosted Service for Azure.

    • deploymentSettings.json:存储供 Azure PowerShell 部署 cmdlet 使用的本地设置。deploymentSettings.json: Stores local settings that are used by the Azure PowerShell deployment cmdlets.

  4. 输入以下命令添加新的 Web 角色:Enter the following command to add a new web role:

    Add-AzureNodeWebRole
    

    Add-AzureNodeWebRole 命令的输出

    Add-azurenodewebrole cmdlet 会创建一个基本 Node.js 应用程序。The Add-AzureNodeWebRole cmdlet creates a basic Node.js application. 它还会修改 .csfg 和 .csdef 文件,以添加新角色的配置条目。It also modifies the .csfg and .csdef files to add configuration entries for the new role.

    Note

    如果不指定角色名称,则使用默认名称。If you do not specify a role name, a default name is used. 可以提供一个名称作为第一个 cmdlet 参数: Add-AzureNodeWebRole MyRoleYou can provide a name as the first cmdlet parameter: Add-AzureNodeWebRole MyRole

Node.js 应用在 server.js 文件中定义,该文件位于 Web 角色(默认为 WebRole1)的目录中。The Node.js app is defined in the file server.js, located in the directory for the web role (WebRole1 by default). 代码如下:Here is the code:

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);

此代码基本上与 nodejs.org 网站上的“Hello World”示例相同,但它使用云环境分配的端口号。This code is essentially the same as the "Hello World" sample on the nodejs.org website, except it uses the port number assigned by the cloud environment.

将应用程序部署到 AzureDeploy the application to Azure

Note

若要完成本教程,需要一个 Azure 帐户。To complete this tutorial, you need an Azure account. 可以激活 MSDN 订阅者权益注册试用帐户You can activate your MSDN subscriber benefits or sign up for a trial account.

下载 Azure 发布设置Download the Azure publishing settings

要将应用程序部署到 Azure,必须先为 Azure 订阅下载发布设置。To deploy your application to Azure, you must first download the publishing settings for your Azure subscription.

  1. 运行以下 Azure Powershell cmdlet:Run the following Azure PowerShell cmdlet:

    Get-AzurePublishSettingsFile -Environment AzureChinaCloud
    

    此操作使用浏览器导航到发布设置下载页。This will use your browser to navigate to the publish settings download page. 可能会提示使用 Microsoft 帐户登录。You may be prompted to log in with a Microsoft Account. 如果出现提示,请使用与 Azure 订阅关联的帐户。If so, use the account associated with your Azure subscription.

    将已下载的配置文件保存到可轻松访问的文件位置。Save the downloaded profile to a file location you can easily access.

  2. 运行以下 cmdlet 以导入下载的发布配置文件:Run following cmdlet to import the publishing profile you downloaded:

    Import-AzurePublishSettingsFile -Environment AzureChinaCloud [path to file]
    

    Note

    导入发布设置之后,请考虑删除下载的 .publishSettings 文件,因为其他人可能利用其中信息访问帐户。After importing the publish settings, consider deleting the downloaded .publishSettings file, because it contains information that could allow someone to access your account.

发布应用程序Publish the application

若要发布,请运行以下命令:To publish, run the following commands:

  $ServiceName = "NodeHelloWorld" + $(Get-Date -Format ('ddhhmm'))   
Publish-AzureServiceProject -ServiceName $ServiceName  -Location "China East" -Launch
  • -ServiceName 指定部署的名称。-ServiceName specifies the name for the deployment. 此名称必须唯一,否则发布过程会失败。This must be a unique name, otherwise the publish process will fail. Get-Date 命令附加应使名称唯一的日期/时间字符串。The Get-Date command tacks on a date/time string that should make the name unique.

  • -Location 指定托管应用程序的数据中心。-Location specifies the datacenter that the application will be hosted in. 若要查看可用数据中心的列表,请使用 Get-AzureLocation cmdlet。To see a list of available datacenters, use the Get-AzureLocation cmdlet.

  • -Launch 用于在部署完成后打开浏览器窗口并导航到托管服务。-Launch opens a browser window and navigates to the hosted service after deployment has completed.

发布成功之后,可看到如下响应:After publishing succeeds, you will see a response similar to the following:

Publish-AzureService 命令的输出

Note

首次发布后应用程序后,它可能需要几分钟才能部署完成并可供使用。It can take several minutes for the application to deploy and become available when first published.

在部署完成后,系统会打开一个浏览器窗口并导航到云服务。Once the deployment has completed, a browser window will open and navigate to the cloud service.

显示“hello world”页面的浏览器窗口;URL 指示该页面托管在 Azure 上。

应用程序现在在 Azure 上运行。Your application is now running on Azure.

Publish-AzureServiceProject cmdlet 执行以下步骤:The Publish-AzureServiceProject cmdlet performs the following steps:

  1. 创建包供部署。Creates a package to deploy. 该包包含应用程序文件夹中的所有文件。The package contains all the files in your application folder.

  2. 如果存储帐户不存在,则创建一个新的存储帐户Creates a new storage account if one does not exist. Azure 存储帐户在部署期间用于存储应用程序包。The Azure storage account is used to store the application package during deployment. 部署完成后,可以安全删除该存储帐户。You can safely delete the storage account after deployment is done.

  3. 如果云服务尚不存在,会创建一个新的云服务Creates a new cloud service if one does not already exist. 云服务 是一个容器,用于在将应用程序部署到 Azure 后托管该应用程序。A cloud service is the container in which your application is hosted when it is deployed to Azure. 有关详细信息,请参阅 创建 Azure 托管服务概述For more information, see Overview of Creating a Hosted Service for Azure.

  4. 将部署包发布到 Azure。Publishes the deployment package to Azure.

停止并删除应用程序Stopping and deleting your application

部署应用程序后,可能需要禁用它以免产生额外费用。After deploying your application, you may want to disable it so you can avoid extra costs. Azure 将按使用的服务器小时数对 Web 角色实例计费。Azure bills web role instances per hour of server time consumed. 应用程序部署后即开始使用服务器时间,即使相关实例未运行并处于停止状态。Server time is consumed once your application is deployed, even if the instances are not running and are in the stopped state.

  1. 在 Windows PowerShell 窗口中,使用以下 cmdlet 以停止上一节中创建的服务部署:In the Windows PowerShell window, stop the service deployment created in the previous section with the following cmdlet:

    Stop-AzureService
    

    停止服务可能需要花费几分钟时间。Stopping the service may take several minutes. 在服务停止时,会收到一条指示服务已停止的消息。When the service is stopped, you receive a message indicating that it has stopped.

    Stop-AzureService 命令的状态

  2. 若要删除服务,请调用以下 cmdlet:To delete the service, call the following cmdlet:

    Remove-AzureService
    

    在出现提示时,输入 Y 以删除服务。When prompted, enter Y to delete the service.

    删除服务可能需要花费几分钟时间。Deleting the service may take several minutes. 删除服务后,将收到一条指示服务已被删除的消息。After the service has been deleted you receive a message indicating that the service was deleted.

    Remove-AzureService 命令的状态

    Note

    删除服务不会删除最初发布服务时创建的存储帐户,用户仍需为使用的存储付费。Deleting the service does not delete the storage account that was created when the service was initially published, and you will continue to be billed for storage used. 如果没有任何其他对象使用存储,请删除它。If nothing else is using the storage, you may want to delete it.

后续步骤Next steps

有关详细信息,请参阅 Node.js 开发人员中心For more information, see the Node.js Developer Center.