Azure Mobile Services has recently released the new Hybrid Connections feature that provides an easy and effective way to connect to on-premises resources from your mobile service, available for both Node.js and .NET backends.
One of the scenarios enabled by Hybrid Connections is utilizing a database hosted outside of Microsoft Azure from your Mobile Service and we have a step-by-step tutorial on how to set it up for apps that use the .NET backend. This post will help you get started with a Node.js backend.
All initial setup steps are the same as for the .NET backend, so please go ahead to that tutorial and go through steps 1 – 4:
- Prerequisites (you don’t need to install Visual Studio, though)
- Install SQL Server Express, enable TCP/IP, and create a SQL Server database on-premises
- Create a Hybrid Connection
- Install the on-premises Hybrid Connection Manager to complete the connection
Some Node.js sample code to get you started
Now that you’ve completed all setup steps, you’re ready to start writing some Node.js code that will connect to your on-premises resources. For example, this is a custom API script that will connect to an on-premises database.
As mentioned in the code comments, you will also need to specify your on-premises connection string as an app-setting in the Configure tab of your Mobile Service in the Azure Portal.
And that’s it! A few things to note in this example:
- It assumes that you already have a table called “MyTable” in the default schema “dbo”. You can easily change the query to use an existing table you might have or, if you don’t have a table yet, you can easily create one using SQL Server Management Studio.
- As the developer, you didn’t have to write any code at all to setup the connection to your on-premises database. We take care of it in the backend for you.
- The connection string is the same you would have used if you were actually running the Node.js code on-premises (in ODBC format). Thanks to BizTalk Hybrid Connections, you can now reuse that same connection string from the cloud securely and conveniently.
- This sample code is meant to show how simple it is to use this new feature, and is not an example of production-quality code. When using this in production, consider implementing better error handling and logging.
We hope this post will help you get started with Hybrid Connections in a Node.js Mobile Service. The main objective was to emphasize how simple it is to set it up and start using it, both from Node.js and .NET backends. We encourage you to play with the new feature and, as usual, to submit questions or comments to this post, the MSDN forums or contact us via Twitter @AzureMobile.