生成 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.

提示

想要构建一个简单的网站?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

备注

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

  • 安装和配置 Azure PowerShellInstall and configure Azure PowerShell.
  • 下载并安装 [用于 .NET 的 Azure SDK - 2.7]。Download and install the [Azure SDK for .NET 2.7]. 在安装设置中,选择:In the install setup, select:
    • MicrosoftAzureAuthoringToolsMicrosoftAzureAuthoringTools
    • MicrosoftAzureComputeEmulatorMicrosoftAzureComputeEmulator

创建 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 PowerShellRun 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.

    备注

    如果不指定角色名称,将使用默认名称。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

备注

要完成本教程,需要一个 Azure 帐户。To complete this tutorial, you need an Azure account. 可以注册试用版You can sign up for a Trial.

下载 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]
    

    备注

    导入发布设置之后,请考虑删除下载的 .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 命令的输出

备注

部署应用程序并在首次发布该程序后使其可供使用可能需要花费几分钟时间。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 命令的状态

    备注

    删除服务不会删除最初发布服务时所创建的存储帐户,并且你仍需为使用的存储付费。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.