您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

使用 Express 在 Azure 云服务中构建并部署 Node.js Web 应用程序Build and deploy a Node.js web application using Express on an Azure Cloud Services

Node.js 包含核心运行时中最小的一个功能集。Node.js includes a minimal set of functionality in the core runtime. 开发人员在开发 Node.js 应用程序时经常会使用第三方模块来提供额外功能。Developers often use 3rd party modules to provide additional functionality when developing a Node.js application. 本教程使用 Express 模块新建一个应用程序,该模块提供了用于创建 Node.js Web 应用程序的 MVC 框架。In this tutorial you will create a new application using the Express module, which provides an MVC framework for creating Node.js web applications.

以下是已完成应用程序的屏幕截图:A screenshot of the completed application is below:

显示“Welcome to Express in Azure”的 Web 浏览器

创建云服务项目Create a Cloud Service Project

备注

若要成功完成此部分,必须为平台安装有效的 Node.js 和 Azure SDK for Node.js。To successfully complete this section, you must have a working installation of Node.js and the Azure SDK for Node.js for your platform.

执行下列步骤可新建一个名为“expressapp”的云服务项目:Perform the following steps to create a new cloud service project named 'expressapp':

  1. 在“开始”菜单或“开始”屏幕中,搜索“Windows PowerShell” 。From the Start Menu or Start Screen, search for Windows PowerShell. 最后,右键单击“Windows PowerShell”并选择“以管理员身份运行” 。Finally, right-click Windows PowerShell and select Run As Administrator.

    Azure PowerShell 图标

  2. 将目录切换到 c:\node 目录,然后输入下列命令,新建一个名为 expressapp 的解决方案和名为 WebRole1 的 Web 角色 :Change directories to the c:\node directory and then enter the following commands to create a new solution named expressapp and a web role named WebRole1:

    PS C:\node> New-AzureServiceProject expressapp
    PS C:\Node\expressapp> Add-AzureNodeWebRole
    PS C:\Node\expressapp> Set-AzureServiceProjectRole WebRole1 Node 0.10.21
    

    备注

    默认情况下, Add-azurenodewebrole 使用旧版 Node.js。By default, Add-AzureNodeWebRole uses an older version of Node.js. 上述 Set-azureserviceprojectrole 语句将指示 Azure 使用 v0.10.21 版本的节点。The Set-AzureServiceProjectRole statement above instructs Azure to use v0.10.21 of Node. 请注意,这些参数区分大小写。Note the parameters are case-sensitive. 通过检查 WebRole1\package.json 中的“engines”属性可以验证是否已选择正确的 Node.js 版本 。You can verify the correct version of Node.js has been selected by checking the engines property in WebRole1\package.json.

安装 ExpressInstall Express

  1. 通过发出以下命令安装 Express 生成器:Install the Express generator by issuing the following command:

    PS C:\node\expressapp> npm install express-generator -g
    

    npm 命令的输出应与以下结果类似。The output of the npm command should look similar to the result below.

    Windows PowerShell 显示 npm install express 命令的输出。

  2. 将目录切换到 WebRole1 目录,并使用 express 命令生成一个新的应用程序:Change directories to the WebRole1 directory and use the express command to generate a new application:

    PS C:\node\expressapp\WebRole1> express
    

    系统会提示是否覆盖之前创建的应用程序。You will be prompted to overwrite your earlier application. 输入 y 或 yes 继续操作 。Enter y or yes to continue. Express 会生成用于生成应用程序的 app.js 文件和一个文件夹结构。Express will generate the app.js file and a folder structure for building your application.

    express 命令的输出

  3. 若要安装在 package.json 文件中定义的其他依赖项,请输入以下命令:To install additional dependencies defined in the package.json file, enter the following command:

    PS C:\node\expressapp\WebRole1> npm install
    

    npm install 命令的输出

  4. 使用以下命令将 bin/www 文件复制到 server.js 。Use the following command to copy the bin/www file to server.js. 这样,云服务便可以找到此应用程序的入口点。This is so the cloud service can find the entry point for this application.

    PS C:\node\expressapp\WebRole1> copy bin/www server.js
    

    完成此命令后,WebRole1 目录中应会生成一个 server.js 文件。After this command completes, you should have a server.js file in the WebRole1 directory.

  5. 修改 server.js 以删除以下行中的“.”字符之一。Modify the server.js to remove one of the '.' characters from the following line.

    var app = require('../app');
    

    完成此项修改后,该行应如下所示。After making this modification, the line should appear as follows.

    var app = require('./app');
    

    之所以需要进行此更改,是因为我们已将文件(以前的 bin/www)移到了所需应用文件所在的同一个目录 。This change is required since we moved the file (formerly bin/www,) to the same directory as the app file being required. 完成此更改后,请保存 server.js 文件。After making this change, save the server.js file.

  6. 使用以下命令以在 Azure 模拟器中运行应用程序:Use the following command to run the application in the Azure emulator:

    PS C:\node\expressapp\WebRole1> Start-AzureEmulator -launch
    

    包含 Welcome to Express 的网页。

修改视图Modifying the View

现在,修改视图以显示消息“Welcome to Express in Azure”。Now modify the view to display the message "Welcome to Express in Azure".

  1. 请输入以下命令来打开 index.jade 文件:Enter the following command to open the index.jade file:

    PS C:\node\expressapp\WebRole1> notepad views/index.jade
    

    index.jade 文件的内容。

    Jade 是 Express 应用程序使用的默认视图引擎。Jade is the default view engine used by Express applications. 有关 Jade 视图引擎的详细信息,请参阅 http://jade-lang.comFor more information on the Jade view engine, see http://jade-lang.com.

  2. 通过追加 in Azure来修改最后一行文本。Modify the last line of text by appending in Azure.

    index.jade 文件,最后一行的内容为:p Welcome to #{title} in Azure

  3. 保存文件并退出记事本。Save the file and exit Notepad.

  4. 刷新浏览器即可看到所做的更改。Refresh your browser and you will see your changes.

    浏览器窗口,其中的页面包含 Welcome to Express in Azure

测试应用程序后,请使用 Stop-AzureEmulator cmdlet 停止模拟器。After testing the application, use the Stop-AzureEmulator cmdlet to stop the emulator.

将应用程序发布到 AzurePublishing the Application to Azure

在 Azure PowerShell 窗口中,可使用 Publish-AzureServiceProject cmdlet 将应用程序部署到云服务In the Azure PowerShell window, use the Publish-AzureServiceProject cmdlet to deploy the application to a cloud service

PS C:\node\expressapp\WebRole1> Publish-AzureServiceProject -ServiceName myexpressapp -Location "East US" -Launch

在部署操作完成后,浏览器将打开并显示该网页。Once the deployment operation completes, your browser will open and display the web page.

Web 浏览器中显示 Express 页。

后续步骤Next steps

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