Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Prepare Azure environments for new workloads—subscriptions, networking, identity, and landing zones
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/services/app-service/templates/recipes/cosmos/source/nodejs.md
1# Cosmos DB Recipe — Node.js — REFERENCE ONLY23## Cosmos SDK Setup45### npm Packages67```bash8npm install @azure/cosmos @azure/identity9```1011### Database Module1213Create `src/cosmosClient.js`:1415```javascript16const { CosmosClient } = require("@azure/cosmos");17const { DefaultAzureCredential } = require("@azure/identity");1819const client = new CosmosClient({20endpoint: process.env.COSMOS_ENDPOINT,21aadCredentials: new DefaultAzureCredential(),22});2324const container = client25.database(process.env.COSMOS_DATABASE_NAME)26.container(process.env.COSMOS_CONTAINER_NAME);2728module.exports = { container };29```3031### CRUD Endpoints3233Add to `src/index.js`:3435```javascript36const { container } = require("./cosmosClient");3738app.get("/api/items", async (req, res) => {39const { resources } = await container.items.readAll().fetchAll();40res.json(resources);41});4243app.post("/api/items", async (req, res) => {44const { resource } = await container.items.create(req.body);45res.status(201).json(resource);46});47```4849## Files to Modify5051| File | Action |52|------|--------|53| `src/cosmosClient.js` | Create — Cosmos client + container reference |54| `src/index.js` | Modify — add CRUD endpoints |55| `package.json` | Modify — add @azure/cosmos, @azure/identity |56