将现有 Node.js Azure 移动服务升级到应用服务Upgrade your existing Node.js Azure Mobile Service to App Service

应用服务移动应用是使用 Azure 生成移动应用程序的新方式。App Service Mobile is a new way to build mobile applications using Azure. 若要了解详细信息,请参阅什么是移动应用?To learn more, see What are Mobile Apps?.

本文介绍如何将现有 Node.js 后端应用程序从 Azure 移动服务升级到新的应用服务移动应用。This article describes how to upgrade an existing Node.js backend application from Azure Mobile Services to a new App Service Mobile Apps. 执行此升级时,现有移动服务应用程序可以继续正常运行。While you perform this upgrade, your existing Mobile Services application can continue to operate. 如果需要升级 Node.js 后端应用程序,请参阅升级 .NET 移动服务If you need to upgrade a Node.js backend application, refer to Upgrading your .NET Mobile Services.

将某个移动后端升级到 Azure 应用服务后,该后端即可访问所有应用服务功能,同时会根据应用服务定价而不是移动服务定价进行计费。When a mobile backend is upgraded to Azure App Service, it has access to all App Service features and are billed according to App Service pricing, not Mobile Services pricing.

迁移与升级Migrate vs. upgrade

有两种方式可从移动服务移至应用服务:迁移或升级。There are two options for moving from Mobile Services to App Service: migration or upgrade. 两种都可赋予访问应用服务中所有托管功能的权限:Either option will give you access to all of the hosting capabilities of App Service:

  • 迁移 服务仅会更改托管移动后端的基础环境。Migrating a service just changes the underlying environment that hosts your mobile backend. 它要求移动客户端或移动服务器项目 无任何代码更改It requires no code changes to either the mobile client or the mobile server project. 若使用自动迁移选项,这会保留 service.azure-mobile.cn URL。If you use the automated migration option, this preserves your service.azure-mobile.cn URL.

  • 升级 服务要求服务器和客户端项目均 出现代码更改 ,但允许使用新的移动 SDK 功能,例如改进后的身份验证和更灵活的服务器项目。Upgrading a service does require code changes to both your server and client project, but allows you to take advantage of new mobile SDK features, such as improvements to authentication and more flexibility for your server project.

移动应用 Node.js 服务器 SDK 改进Improvements in Mobile Apps Node.js server SDK

升级到新版移动应用 SDK 可获得许多改进,包括:Upgrading to the new Mobile Apps SDK provides many improvements, including:

  • 新的轻量型 Node SDK 基于 Express 框架,与新推出的 Node 版本功能保持一致。可以使用 Express 中间件自定义应用程序行为。Based on the Express framework, the new Node SDK is light-weight and designed to keep up with new Node versions as they come out. You can customize the application behavior with Express middleware.
  • 移动服务 SDK 相比,性能有明显改进。Significant performance improvements compared to the Mobile Services SDK.
  • 现在,可以将网站与移动后端托管在一起;同样,可以很轻松地将 Azure 移动 SDK 添加到任何现有 express.v4 应用程序。You can now host a website together with your mobile backend; similarly, it's easy to add the Azure Mobile SDK to any existing express.v4 application.
  • 移动应用 SDK 为跨平台和本地开发而构建,可以在 Windows、Linux 和 OSX 平台上本地开发与运行。Built for cross-platform and local development, the Mobile Apps SDK can be developed and run locally on Windows, Linux, and OSX platforms. 现在,可以方便地使用常见的 Node 开发技术,例如,在部署之前运行 Mocha 测试。It's now easy to use common Node development techniques like running Mocha tests prior to deployment.

基本升级概述Basic upgrade overview

为了帮助升级 Node.js 后端,Azure 应用服务提供了兼容包。To aid in upgrading a Node.js backend, Azure App Service has provided a compatibility package. 升级后,会获得可部署到新应用服务站点的全新站点。After upgrade, you will have a new site that can be deployed to a new App Service site.

移动服务客户端 SDK 与新的移动应用服务器 SDK 兼容。The Mobile Services client SDKs are not compatible with the new Mobile Apps server SDK. 为了提供应用程序的服务连续性,不应该将更改发布到当前正在为发布的客户端提供服务的站点。In order to provide continuity of service for your app, you should not publish changes to a site currently serving published clients. 而应该创建新的移动应用作为副本。Instead, you should create a new mobile app that serves as a duplicate. 可以在同一个应用服务计划中放置此应用程序,以免产生额外的财务成本。You can put this application on the same App Service plan to avoid incurring additional financial cost.

这样,就会获得两个版本的应用程序:一个保持不变并为已发布的现有应用程序提供服务,另一个则可升级且目标为新的客户端版本。You will then have two versions of the application: one that stays the same and serves published apps in the wild, and the other which you can then upgrade and target with a new client release. 可根据自己的步调移动和测试代码,但应确保所做的任何 bug 修复都已应用到这两个版本。You can move and test your code at your pace, but you should make sure that any bug fixes you make get applied to both. 觉得已将所需数量的现有客户端应用升级到最新版本后,可以根据需要删除已迁移的原始应用。Once you feel that a desired number of client apps in the wild have updated to the latest version, you can delete the original migrated app if you desire. 如果托管在与移动移动相同的应用服务计划中,则不会产生任何额外的成本。It doesn't incur any additional monetary costs, if hosted in the same App Service plan as your Mobile App.

完整的升级过程大致如下:The full outline for the upgrade process is as follows:

  1. 下载现有的(已迁移)Azure 移动服务。Download your existing (migrated) Azure Mobile Service.
  2. 使用兼容包将项目转换为 Azure 移动应用。Convert the project to an Azure Mobile App using the compatibility package.
  3. 更正任何差异(例如身份验证设置)。Correct any differences (such as authentication settings).
  4. 将转换后的 Azure 移动应用项目部署到新的 应用服务。Deploy your converted Azure Mobile App project to a new App Service.
  5. 发布使用新移动应用的新版客户端应用程序。Release a new version of your client application that uses the new Mobile App.
  6. (可选)删除已迁移的原始移动服务应用。(Optional) Delete your original migrated mobile service app.

当已迁移的原始移动服务没有任何流量时即可删除。Deletion can occur when you don't see any traffic on your original migrated mobile service.

安装必备组件Install the Pre-requisites

应在本地计算机上安装 [Node]。You should install [Node] on your local machine. 还应安装兼容包。You should also install the compatibility package. 安装 Node 后,可以从新的 cmd 或 PowerShell 命令提示符运行以下命令:After Node is installed, you can run the following command from a new cmd or PowerShell prompt:

npm i -g azure-mobile-apps-compatibility

获取 Azure 移动服务脚本Obtain your Azure Mobile Services Scripts

  • 登录到 Azure 门户Log in to the Azure Portal.
  • 使用“所有资源”或“应用服务”找到移动服务站点。Using All Resources or App Services, find your Mobile Services site.
  • 在站点内单击“工具” -> “Kudu” -> “转到”,打开 Kudu 站点。Within the site, click on Tools -> Kudu -> Go to open the Kudu site.
  • 单击“调试控制台” -> “PowerShell”打开调试控制台。Click on Debug Console -> PowerShell to open the Debug console.
  • 依次单击每个目录导航到 site/wwwroot/App_Data/configNavigate to site/wwwroot/App_Data/config by clicking on each directory in turn
  • 单击 scripts 目录旁边的下载图标。Click on the download icon next to the scripts directory.

随后会下载 ZIP 格式的脚本。This will download the scripts in ZIP format. 在本地计算机上创建新目录,并在该目录中解压缩 scripts.ZIP 文件。Create a new directory on your local machine and unpack the scripts.ZIP file within the directory. 此时会创建 scripts 目录。This will create a scripts directory.

创建新 Azure 移动应用后端的基架Scaffold the new Azure Mobile Apps backend

从包含脚本目录的目录运行以下命令:Run the following command from the directory containing the scripts directory:

scaffold-mobile-app scripts out

此时会在 out 目录中创建带有基架的 Azure 移动应用后端。This will create a scaffolded Azure Mobile Apps backend in the out directory. 最好将 out 目录签入所选的源代码存储库(但不一定要这样做)。Although not required, it's a good idea to check the out directory into a source code repository of your choice.

部署 Azure 移动应用后端Deploy your Azure Mobile Apps backend

在部署期间,需要执行以下操作:During deployment, you will need to do the following:

  1. Azure 门户中创建新的移动应用。Create a new Mobile App in the Azure Portal.
  2. 对连接的数据库运行 createViews.sql 脚本。Run the createViews.sql script on your connected database.
  3. 将链接到移动服务的数据库链接到新的应用服务。Link the database that is linked to your Mobile Service to your new App Service.
  4. 将任何其他资源(例如通知中心)链接到新的应用服务。Link any other resources (such as Notification Hubs) to the new App Service.
  5. 将生成的代码部署到新站点。Deploy the generated code to your new site.

创建新的移动应用Create a new Mobile App

  1. Azure 门户登录。Log in at the Azure Portal.

  2. 单击“+新建” > “Web + 移动” > “移动应用”,然后提供移动应用后端的名称。Click +NEW > Web + Mobile > Mobile App, then provide a name for your Mobile App backend.

  3. 对于“资源组”,请选择现有资源组,或创建新组(使用与应用相同的名称。)For the Resource Group, select an existing resource group, or create a new one (using the same name as your app.)

    可以选择其他应用服务计划或创建新的计划。You can either select another App Service plan or create a new one. 若要深入了解应用服务计划以及如何在不同定价层和所需位置中创建新计划,请参阅 Azure App Service 计划深入概述For more about App Services plans and how to create a new plan in a different pricing tier and in your desired location, see Azure App Service plans in-depth overview.

  4. 对于“应用服务计划”,请选择默认计划(位于标准层)。For the App Service plan, the default plan (in the Standard tier) is selected. 还可以选择其他计划,或创建一个新计划You can also select a different plan, or create a new one. 应用服务计划的设置将确定与应用关联的位置、功能、成本和计算资源The App Service plan's settings determine the location, features, cost and compute resources associated with your app.

    做出有关计划的决定后,单击“创建” 。After you decide on the plan, click Create. 这会创建移动应用后端。This creates the Mobile App backend.

运行 CreateViews.SQLRun CreateViews.SQL

带有基架的应用包含名为 createViews.sql的文件。The scaffolded app contains a file called createViews.sql. 必须对目标数据库执行此脚本。This script must be executed against the target database. 可以在“设置”页的“连接字符串”下从已迁移的移动服务获取目标数据库的连接字符串。The connection string for the target database can be obtained from your migrated mobile service from the Settings page under Connection Strings. MS_TableConnectionStringIt is named MS_TableConnectionString.

可以从 SQL Server Management Studio 或 Visual Studio 内部运行此脚本。You can run this script from within SQL Server Management Studio or Visual Studio.

将现有数据库链接到应用服务:Link the existing database to your App Service:

  • Azure 门户中,打开应用服务。In the Azure Portal, open your App Service.
  • 选择“所有设置” -> “数据连接”。Select All Settings -> Data Connections.
  • 单击“+添加”。Click on + Add.
  • 在下拉列表中,选择“SQL 数据库” In the drop-down, select SQL Database
  • 在“SQL 数据库”下,选择现有数据库,然后单击“选择”。Under SQL Database, select your existing database, then click on Select.
  • 在“连接字符串”下,输入数据库的用户名和密码,然后单击“确定”。Under Connection string, enter the username and password for the database, then click on OK.
  • 在“添加数据连接”页中,单击“确定”。In the Add data connections page, click on OK.

查看已迁移的移动服务中目标数据库的连接字符串,即可找到用户名和密码。The username and password can be found by viewing the Connection String for the target database in your migrated Mobile Service.

设置身份验证Set up authentication

Azure 移动应用允许在服务中配置 Azure Active Directory 和 Microsoft 身份验证。Azure Mobile Apps allows you to configure Azure Active Directory and Microsoft authentication within the service. 自定义身份验证需要另行开发。Custom authentication will need to be developed separately. 有关详细信息,请参阅身份验证概念文档和身份验证快速入门文档。Refer to the Authentication Concepts documentation and Authentication Quickstart documentation for more information.

更新移动客户端Update Mobile clients

在获得可正常运行的移动应用后端之后,可以在使用它的新版客户端应用程序上操作。Once you have an operational Mobile App backend, you can work on a new version of your client application which consumes it. 移动应用还包含新版客户端 SDK。与上述服务器升级类似,需先删除对移动服务 SDK 的所有引用,然后再安装移动应用版本。Mobile Apps also includes a new version of the client SDKs, and similar to the server upgrade above, you will need to remove all references to the Mobile Services SDKs before installing the Mobile Apps versions.

版本间的其中一个主要更改是构造函数不再需要应用程序密钥。One of the main changes between the versions is that the constructors no longer require an application key. 现在只需传入移动应用的 URL。You now simply pass in the URL of your Mobile App. 例如,在 .NET 客户端中, MobileServiceClient 构造函数现在是:For example, on the .NET clients, the MobileServiceClient constructor is now:

    public static MobileServiceClient MobileService = new MobileServiceClient(
        "https://contoso.chinacloudsites.cn", // URL of the Mobile App
    );

可以通过以下链接,阅读有关安装新 SDK 以及使用新结构的信息:You can read about installing the new SDKs and using the new structure via the links below:

如果应用程序使用推送通知,请记下每个平台的特定注册说明,因为相关的注册也发生了一些更改。If your application makes use of push notifications, make note of the specific registration instructions for each platform, as there have been some changes there as well.

准备好新客户端版本后,请尝试对已升级的服务器项目运行该版本。When you have the new client version ready, try it out against your upgraded server project. 确认该版本正常工作后,可将新版应用程序发布给客户。After validating that it works, you can release a new version of your application to customers. 最后,在客户收到这些更新后,便可以删除应用的移动服务版本。Eventually, once your customers have had a chance to receive these updates, you can delete the Mobile Services version of your app. 现在,已使用最新的移动应用服务器 SDK 完全升级到应用服务移动应用。At this point, you have completely upgraded to an App Service Mobile App using the latest Mobile Apps server SDK.