services | platforms | author |
---|---|---|
cosmos-db |
nodejs |
ryancrawcour |
This sample shows you how to use the Azure Cosmos DB service to store and access data from a Node.js Express application hosted on Azure Websites.
For a complete end-to-end walk-through of creating this application, please refer to the full tutorial on the Azure documentation page
-
Before you can run this sample, you must have the following perquisites:
- An active Azure Cosmos DB account - If you don't have an account, refer to the Create an Azure Cosmos DB account article.
- Node.js version v0.10.29 or higher.
- Express generator (you can install this via npm install express-generator -g)
- Git.
-
Clone this repository, or download the zip file.
-
Retrieve the URI and PRIMARY KEY (or SECONDARY KEY) values from the Keys blade of your Azure Cosmos DB account in the Azure portal. For more information on obtaining endpoint & keys for your Azure Cosmos DB account refer to How to manage an Azure Cosmos DB account.
If you don't have an account, see Create an Azure Cosmos DB database account to set one up.
-
In the config.js file, located in the src folder, find config.host and config.authKey and replace the placeholder values with the values obtained for your account.
-
Install the dependencies by opening a terminal, navigating to the directory in which the sample was installed, and running npm install.
-
In the terminal, run npm start to start your start your node application.
-
Launch a browser and navigate to http://127.0.0.1:3000/
-
If you haven't already, enable a git repository for your Azure Website. You can find instructions on how to do this here.
-
Add your Azure Website as a git remote.
git remote add azure https://[email protected]:443/your-website.git
-
Deploy by pushing to the remote.
git push azure master
-
In a few seconds, git will finish publishing your web application and launch a browser where you can see your handy work running in Azure!
The code included in this sample is intended to get you going with a simple Node.js Express application that connects to Azure Cosmos DB with the express intent of demonstrating how to interact with Azure Cosmos DB using the documentdb npm package. It is not intended to be a set of best practices on how to build scalable enterprise grade web applications. This is beyond the scope of this quick start sample.