You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: "Tutorial: Access data in Azure Cosmos DB using Mongoose with Azure Static Web Apps"
2
+
title: "Access data in Azure Cosmos DB using Mongoose with Azure Static Web Apps"
3
3
description: Learn to access data in Azure Cosmos DB using Mongoose from an Azure Static Web Apps API function.
4
4
author: GeekTrainer
5
5
ms.author: chrhar
6
6
ms.service: static-web-apps
7
7
ms.custom: ignite-2022
8
8
ms.topic: tutorial
9
-
ms.date: 01/25/2021
9
+
ms.date: 10/10/2022
10
10
---
11
11
12
-
# Tutorial: Access data in Azure Cosmos DB using Mongoose with Azure Static Web Apps
13
-
14
-
[Mongoose](https://mongoosejs.com/) is the most popular ODM (Object Document Mapping) client for Node.js. Allowing you to design a data structure and enforce validation, Mongoose provides all the tooling necessary to interact with databases that support the MongoDB API. [Azure Cosmos DB for MongoDB](../cosmos-db/mongodb-introduction.md) supports the necessary MongoDB APIs and is available as a back-end server option on Azure.
15
-
16
-
In this tutorial, you learn how to:
17
-
18
-
> [!div class="checklist"]
19
-
> - Create an Azure Cosmos DB serverless account
20
-
> - Create Azure Static Web Apps
21
-
> - Update application settings to store the connection string
22
-
23
-
If you don’t have an Azure subscription, create a [free trial account](https://azure.microsoft.com/free/).
12
+
# Access data in Azure Cosmos DB using Mongoose with Azure Static Web Apps
24
13
14
+
[Mongoose](https://mongoosejs.com/) is the most popular ODM (Object Document Mapping) client for Node.js. Mongoose allows you to design a data structure and enforce validation, and provides all the tooling necessary to interact with databases that support the MongoDB API. [Cosmos DB](../cosmos-db/mongodb-introduction.md) supports the necessary MongoDB APIs and is available as a back-end server option on Azure.
25
15
## Prerequisites
26
16
27
-
- An [Azure account](https://azure.microsoft.com/free/)
28
-
- A [GitHub account](https://github.com/join)
29
-
30
-
## Sign in to Azure
31
-
32
-
Sign in to the [Azure portal](https://portal.azure.com).
17
+
- An [Azure account](https://azure.microsoft.com/free/). If you don’t have an Azure subscription, create a [free trial account](https://azure.microsoft.com/free/).
18
+
- A [GitHub account](https://github.com/join).
19
+
- A [Cosmos DB serverless](../cosmos-db/serverless.md) account. With a serverless account, you only pay for the resources as they're used and avoid needing to create a full infrastructure.
20
+
## 1. Create a Cosmos DB serverless database
33
21
34
-
## Create an Azure Cosmos DB serverless database
22
+
Complete the following steps to create a Cosmos serverless DB.
35
23
36
-
Begin by creating an [Azure Cosmos DB serverless](../cosmos-db/serverless.md) account. By using a serverless account, you only pay for the resources as they are used and avoid needing to create a full infrastructure.
37
-
38
-
1. Navigate to the [Azure portal](https://portal.azure.com)
39
-
2. Select **Create a resource**
40
-
3. Enter **Azure Cosmos DB** in the search box
41
-
4. Select **Azure Cosmos DB**
42
-
5. Select **Create**
43
-
6. If prompted, under **Azure Cosmos DB for MongoDB** select **Create**
44
-
7. Configure your Azure Cosmos DB Account with the following information
24
+
1. Sign in to the [Azure portal](https://portal.azure.com).
25
+
2. Select **Create a resource**.
26
+
3. Enter *Azure Cosmos DB* in the search box.
27
+
4. Select **Azure Cosmos DB**.
28
+
5. Select **Create**.
29
+
6. If prompted, under **Azure Cosmos DB API for MongoDB** select **Create**.
30
+
7. Configure your Azure Cosmos DB Account with the following information:
45
31
- Subscription: Choose the subscription you wish to use
46
32
- Resource: Select **Create new**, and set the name to **aswa-mongoose**
47
33
- Account name: A unique value is required
48
34
- Location: **West US 2**
49
35
- Capacity mode: **Serverless (preview)**
50
36
- Version: **4.0**
51
-
:::image type="content" source="media/add-mongoose/cosmos-db.png" alt-text="Create new Azure Cosmos DB instance":::
52
-
8. Select **Review + create**
53
-
9. Select **Create**
37
+
:::image type="content" source="media/add-mongoose/cosmos-db.png" alt-text="Screenshot showing form for creating new Cosmos DB instance.":::
38
+
8. Select **Review + create**.
39
+
9. Select **Create**.
54
40
55
-
The creation process will take a few minutes. Later steps will return to the database to gather the connection string.
41
+
The creation process takes a few minutes. We'll come back to the database to gather the connection string after we [create a static web app](#2-create-a-static-web-app).
56
42
57
-
## Create a static web app
43
+
## 2. Create a static web app
58
44
59
45
This tutorial uses a GitHub template repository to help you create your application.
60
46
61
-
1.Navigate to the [starter template](https://github.com/login?return_to=/staticwebdev/mongoose-starter/generate)
62
-
2. Choose the **owner** (if using an organization other than your main account)
63
-
3. Name your repository **aswa-mongoose-tutorial**
64
-
4. Select **Create repository from template**
65
-
5. Return to the [Azure portal](https://portal.azure.com)
66
-
6. Select **Create a resource**
67
-
7.Type**static web app** in the search box
68
-
8. Select **Static Web App**
69
-
9. Select **Create**
70
-
10. Configure your Azure Static Web App with the following information
47
+
1.Go to the [starter template](https://github.com/login?return_to=/staticwebdev/mongoose-starter/generate).
48
+
2. Choose the **owner** (if using an organization other than your main account).
49
+
3. Name your repository **aswa-mongoose-tutorial**.
50
+
4. Select **Create repository from template**.
51
+
5. Return to the [Azure portal](https://portal.azure.com).
52
+
6. Select **Create a resource**.
53
+
7.Enter**static web app** in the search box.
54
+
8. Select **Static Web App**.
55
+
9. Select **Create**.
56
+
10. Configure your Azure Static Web App with the following information:
71
57
- Subscription: Choose the same subscription as before
72
58
- Resource group: Select **aswa-mongoose**
73
59
- Name: **aswa-mongoose-tutorial**
@@ -82,61 +68,63 @@ This tutorial uses a GitHub template repository to help you create your applicat
82
68
- Api location: **api**
83
69
- Output location: **build**
84
70
:::image type="content" source="media/add-mongoose/azure-static-web-apps.png" alt-text="Completed Azure Static Web Apps form":::
85
-
11. Select **Review and create**
86
-
12. Select **Create**
87
-
13. The creation process takes a few moments; select **Go to resource** once the static web app is provisioned
88
-
89
-
## Configure database connection string
90
-
91
-
In order to allow the web app to communicate with the database, the database connection string is stored as an [Application Setting](application-settings.md). Setting values are accessible in Node.js using the `process.env` object.
92
-
93
-
1. Select **Home** in the upper left corner of the Azure portal (or navigate back to [https://portal.azure.com](https://portal.azure.com))
94
-
2. Select **Resource groups**
95
-
3. Select **aswa-mongoose**
96
-
4. Select the name of your database account - it will have a type of **Azure Cosmos DB for Mongo DB**
97
-
5. Under **Settings** select **Connection String**
98
-
6. Copy the connection string listed under **PRIMARY CONNECTION STRING**
99
-
7. In the breadcrumbs, select **aswa-mongoose**
100
-
8. Select **aswa-mongoose-tutorial** to return to the website instance
101
-
9. Under **Settings** select **Configuration**
102
-
10. Select **Add** and create a new Application Setting with the following values
71
+
72
+
11. Select **Review and create**.
73
+
12. Select **Create**.
74
+
13. The creation process takes a few moments; select **Go to resource** once the static web app is provisioned.
75
+
76
+
## 3. Configure database connection string
77
+
78
+
To allow the web app to communicate with the database, the database connection string is stored as an [application setting](application-settings.md). Setting values are accessible in Node.js using the `process.env` object.
79
+
80
+
1. Select **Home** in the upper left corner of the Azure portal (or go back to [https://portal.azure.com](https://portal.azure.com)).
81
+
2. Select **Resource groups**.
82
+
3. Select **aswa-mongoose**.
83
+
4. Select the name of your database account - it has a type of **Azure Cosmos DB API for Mongo DB**.
84
+
5. Under **Settings** select **Connection String**.
85
+
6. Copy the connection string listed under **PRIMARY CONNECTION STRING**.
86
+
7. In the breadcrumbs, select **aswa-mongoose**.
87
+
8. Select **aswa-mongoose-tutorial** to return to the website instance.
88
+
9. Under **Settings** select **Configuration**.
89
+
10. Select **Add** and create a new Application Setting with the following values:
103
90
- Name: **AZURE_COSMOS_CONNECTION_STRING**
104
91
- Value: \<Paste the connection string you copied earlier\>
105
-
11. Select **OK**
106
-
12. Select **Add** and create a new Application Setting with the following values for name of the database
92
+
11. Select **OK**.
93
+
12. Select **Add** and create a new Application Setting with the following values for name of the database:
107
94
- Name: **AZURE_COSMOS_DATABASE_NAME**
108
95
- Value: **todo**
109
-
13. Select **Save**
96
+
13. Select **OK**.
97
+
14. Select **Save**.
110
98
111
-
## Navigate to your site
99
+
## 4. Go to your site
112
100
113
101
You can now explore the static web app.
114
102
115
-
1.Select **Overview**
116
-
1. Select the URL displayed in the upper right
117
-
1. It will look similar to `https://calm-pond-05fcdb.azurestaticapps.net`
118
-
1. Select **Please login to see your list of tasks**
119
-
1. Select **Grant consent** to access the application
120
-
1. Create a new lists by typing a name into the textbox labeled **create new list** and selecting **Save**
121
-
1. Create a new task by typing in a title in the textbox labeled **create new item** and selecting **Save**
122
-
1. Confirm the task is displayed (it may take a moment)
123
-
1. Mark the task as complete by **selecting the check**; the task will be moved to the **Done items** section of the page
124
-
1.**Refresh the page** to confirm a database is being used
103
+
1.In the Azure portal, select **Overview**.
104
+
2. Select the URL displayed in the upper right.
105
+
1. It looks similar to `https://calm-pond-05fcdb.azurestaticapps.net`.
106
+
3. Select **Please login to see your list of tasks**.
107
+
4. Select **Grant consent** to access the application.
108
+
5. Create a new list by entering a name into the textbox labeled **create new list** and selecting **Save**.
109
+
6. Create a new task by typing in a title in the textbox labeled **create new item** and selecting **Save**.
110
+
7. Confirm the task is displayed (it may take a moment).
111
+
8. Mark the task as complete by **selecting the check**; the task moves to the **Done items** section of the page.
112
+
9.**Refresh the page** to confirm a database is being used.
125
113
126
114
## Clean up resources
127
115
128
116
If you're not going to continue to use this application, delete
129
117
the resource group with the following steps:
130
118
131
-
1. Return to the [Azure portal](https://portal.azure.com)
132
-
2. Select **Resource groups**
133
-
3. Select **aswa-mongoose**
134
-
4. Select **Delete resource group**
135
-
5.Type**aswa-mongoose** into the textbox
136
-
6. Select **Delete**
119
+
1. Return to the [Azure portal](https://portal.azure.com).
120
+
2. Select **Resource groups**.
121
+
3. Select **aswa-mongoose**.
122
+
4. Select **Delete resource group**.
123
+
5.Enter**aswa-mongoose** into the textbox.
124
+
6. Select **Delete**.
137
125
138
126
## Next steps
139
127
140
128
Advance to the next article to learn how to configure local development...
141
129
> [!div class="nextstepaction"]
142
-
> [Setup local development](./local-development.md)
130
+
> [Set up local development](./local-development.md)
Copy file name to clipboardExpand all lines: articles/static-web-apps/apex-domain-azure-dns.md
+14-14Lines changed: 14 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,17 +21,17 @@ The following procedure requires you to copy settings from an Azure DNS zone you
21
21
22
22
1. Open the [Azure portal](https://portal.azure.com).
23
23
24
-
1.Navigate to your static web app.
24
+
1.Go to your static web app.
25
25
26
26
1. Under *Settings*, select **Custom domains**.
27
27
28
-
1. Select the **+ Add** button, and select **Custom Domain on Azure DNS** from the drop down.
28
+
1. Select **+ Add**, and then select **Custom Domain on Azure DNS** from the drop down menu.
29
29
30
30
1. In the *Enter domain* tab, enter your apex domain name.
31
31
32
32
For instance, if your domain name is `example.com`, enter `example.com` into this box (without any subdomains).
33
33
34
-
1. Select the **Next** button.
34
+
1. Select **Next**.
35
35
36
36
1. In the *Validate + Configure* tab, enter the following values.
37
37
@@ -40,17 +40,17 @@ The following procedure requires you to copy settings from an Azure DNS zone you
40
40
| Domain name | This value should match the domain name you entered in the previous step. |
41
41
| Hostname record type | Select **TXT**. |
42
42
43
-
1. Select the **Generate code** button.
43
+
1. Select **Generate code**.
44
44
45
45
Wait as the code is generated. It make take a minute or so to complete.
46
46
47
-
1. Once the `TXT` record value is generated, select the **copy button** (next to the generated value) to copy the code to the clipboard.
47
+
1. Once the `TXT` record value is generated, **copy** (next to the generated value) the code to the clipboard.
48
48
49
-
1. Select the **Close** button.
49
+
1. Select **Close**.
50
50
51
-
1.Navigate to your Azure DNS zone instance.
51
+
1.Go to your Azure DNS zone instance.
52
52
53
-
1. Select the **+ Record set** button.
53
+
1. Select **+ Record set**.
54
54
55
55
1. Enter the following values in the *Add record set* window.
56
56
@@ -62,23 +62,23 @@ The following procedure requires you to copy settings from an Azure DNS zone you
62
62
| TTL unit | Keep default value. |
63
63
| Value | Paste in the `TXT` record value in your clipboard from your static web app. |
64
64
65
-
1. Select the **OK** button.
65
+
1. Select **OK**.
66
66
67
67
1. Return to your static web app in the Azure portal.
68
68
69
69
1. Under *Settings*, select **Custom domains**.
70
70
71
-
Observe the *Status* for the row of your apex domain. Once the validation is complete, then your apex domain will be publicly available.
71
+
Observe the *Status* for the row of your apex domain. Once the validation is complete, then your apex domain is publicly available.
72
72
73
73
While this validation is running, create an ALIAS record to finalize the configuration.
74
74
75
75
## Set up ALIAS record
76
76
77
77
1. Return to the Azure DNS zone in the Azure portal.
78
78
79
-
1. Select the **+ Record set button** button.
79
+
2. Select **+ Record set**.
80
80
81
-
1. Enter the following values in the *Add record set* window.
81
+
3. Enter the following values in the *Add record set* window.
82
82
83
83
| Setting | Property |
84
84
|---|---|
@@ -91,9 +91,9 @@ While this validation is running, create an ALIAS record to finalize the configu
91
91
| TTL | Keep default value. |
92
92
| TTL unit | Keep default value. |
93
93
94
-
1. Select the **OK** button.
94
+
4. Select **OK**.
95
95
96
-
1. Open a new browser tab and navigate to your apex domain.
96
+
5. Open a new browser tab and go to your apex domain.
97
97
98
98
After the DNS records are updated, you should see your static web app in the browser. Also, inspect the location to verify that your site is served securely using `https`.
Copy file name to clipboardExpand all lines: articles/static-web-apps/apex-domain-external.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,40 +36,40 @@ Before you create the `ALIAS` record, you first need to validate that you own th
36
36
37
37
1. Open the [Azure portal](https://portal.azure.com).
38
38
39
-
1.Navigate to your static web app.
39
+
1.Go to your static web app.
40
40
41
41
1. From the *Overview* window, copy the generated **URL** of your site and set it aside in a text editor for future use.
42
42
43
43
1. Under *Settings*, select **Custom domains**.
44
44
45
-
1. Select the **+ Add** button.
45
+
2. Select **+ Add**.
46
46
47
-
1. In the *Enter domain* tab, enter your apex domain name.
47
+
3. In the *Enter domain* tab, enter your apex domain name.
48
48
49
49
For instance, if your domain name is `example.com`, enter `example.com` into this box (without any subdomains).
50
50
51
-
1. Select the **Next** button.
51
+
4. Select **Next**.
52
52
53
-
1. In the *Validate + Configure* tab, enter the following values.
53
+
5. In the *Validate + Configure* tab, enter the following values.
54
54
55
55
| Setting | Value |
56
56
|---|---|
57
57
| Domain name | This value should match the domain name you entered in the previous step. |
58
58
| Hostname record type | Select **TXT**. |
59
59
60
-
1. Select the **Generate code** button.
60
+
6. Select **Generate code**.
61
61
62
62
Wait as the code is generated. It make take a minute or so to complete.
63
63
64
-
1. Once the `TXT` record value is generated, select the **copy button** (next to the generated value) to copy the code to the clipboard.
64
+
7. Once the `TXT` record value is generated, **copy** (next to the generated value) the code to the clipboard.
65
65
66
-
1. Select the **Close** button.
66
+
8. Select **Close**.
67
67
68
-
1. Open a new browser tab and sign in to your domain registrar account.
68
+
9. Open a new browser tab and sign in to your domain registrar account.
69
69
70
-
1. Navigate to your domain name's DNS configuration settings.
70
+
10. Go to your domain name's DNS configuration settings.
71
71
72
-
1. Add a new `TXT` record with the following values.
72
+
11. Add a new `TXT` record with the following values.
73
73
74
74
| Setting | Value |
75
75
|--|--|
@@ -78,7 +78,7 @@ Before you create the `ALIAS` record, you first need to validate that you own th
78
78
| Value | Paste the generated code value you copied from the Azure portal. |
79
79
| TTL (if applicable) | Leave as default value. |
80
80
81
-
1. Save changes to your DNS record.
81
+
12. Save changes to your DNS record.
82
82
83
83
### Set up an ALIAS record
84
84
@@ -97,7 +97,7 @@ Before you create the `ALIAS` record, you first need to validate that you own th
97
97
98
98
Since DNS settings need to propagate, this process can take some time to complete.
99
99
100
-
1. Open a new browser tab and navigate to your apex domain.
100
+
1. Open a new browser tab and go to your apex domain.
101
101
102
102
After the DNS records are updated, you should see your static web app in the browser. Also, inspect the location to verify that your site is served securely using `https`.
Copy file name to clipboardExpand all lines: articles/static-web-apps/apis-api-management.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ All Azure API Management pricing tiers are available for use with Azure Static W
30
30
31
31
To link an Azure API Management service as the API backend for a static web app, follow these steps:
32
32
33
-
1. In the Azure portal, navigate to the static web app.
33
+
1. In the Azure portal, go to the static web app.
34
34
35
35
1. Select **APIs** from the navigation menu.
36
36
@@ -69,13 +69,13 @@ The linking process also automatically applies the following configuration to yo
69
69
* The linked static web app is configured to include the subscription's primary key and a valid access token when proxying requests to the API Management service.
70
70
71
71
> [!IMPORTANT]
72
-
> Changing the *validate-jwt* policy or regenerating the subscription's primary key will prevent your static web app from proxying requests to the API Management service. Do not modify or delete the subscription or product associated with your static web app while they are linked.
72
+
> Changing the *validate-jwt* policy or regenerating the subscription's primary key prevents your static web app from proxying requests to the API Management service. Do not modify or delete the subscription or product associated with your static web app while they are linked.
73
73
74
74
## Unlink an Azure API Management service
75
75
76
76
To unlink an Azure API Management service from a static web app, follow these steps:
77
77
78
-
1. In the Azure portal, navigate to the static web app.
78
+
1. In the Azure portal, go to the static web app.
79
79
80
80
1. Locate the environment that you want to unlink and select the API Management service name.
0 commit comments