This Azure Function fetches data from an Azure Cosmos DB instance and dynamically serves the data either as JSON or HTML, depending on the query parameters provided in the request.
- Fetches data from Azure Cosmos DB using the Cosmos DB SDK.
- Cleans and formats the fetched data.
- Supports rendering the data as JSON or HTML (using a JSON Resume theme).
- Handles missing or malformed data gracefully.
- Logs detailed information for debugging.
-
Azure Cosmos DB:
- A Cosmos DB account with a database and container set up.
- Required environment variables:
COSMOS_DB_ENDPOINT: The endpoint URL of the Cosmos DB account.COSMOS_DB_KEY: The primary key for authentication.COSMOS_DB_DATABASE: The name of the database.COSMOS_DB_CONTAINER: The name of the container.
-
Azure Function App:
- A Node.js-based Azure Function App.
- Required tools:
-
Clone the repository:
git clone <repository-url> cd <repository-folder>
-
Install dependencies:
npm install
-
Set up local environment variables: Create a
.envfile in the root directory with the following variables:COSMOS_DB_ENDPOINT=<Your Cosmos DB Endpoint> COSMOS_DB_KEY=<Your Cosmos DB Key> COSMOS_DB_DATABASE=<Your Database Name> COSMOS_DB_CONTAINER=<Your Container Name>
-
Run the function locally:
func start
-
Make a request to the function:
- Fetch data as JSON:
curl "http://localhost:7071/api/<function-name>?id=<document-id>" - Fetch data as HTML:
curl "http://localhost:7071/api/<function-name>?id=<document-id>&route=html"
- Fetch data as JSON:
The following environment variables must be defined for the function to connect to Cosmos DB:
COSMOS_DB_ENDPOINT: Cosmos DB account endpoint.COSMOS_DB_KEY: Authentication key for Cosmos DB.COSMOS_DB_DATABASE: Database name.COSMOS_DB_CONTAINER: Container name.
-
Initialize Cosmos DB Client:
const client = new CosmosClient({ endpoint, key });
-
Fetch Data from Cosmos DB:
- Using the document ID and partition key:
const { resource: jsonData } = await container.item(id, partitionKey).read();
- Using the document ID and partition key:
-
Clean Fetched Data:
- Remove metadata fields from the fetched document:
delete cleanJsonData.id; delete cleanJsonData._rid; delete cleanJsonData._self; delete cleanJsonData._etag; delete cleanJsonData._attachments; delete cleanJsonData._ts;
- Remove metadata fields from the fetched document:
-
Serve Data as JSON or HTML:
- Render HTML using the
jsonresume-theme-elegantpackage:const html = render(cleanJsonData);
- Return JSON response:
context.res = { status: 200, headers: { "Content-Type": "application/json" }, body: cleanJsonData, };
- Render HTML using the
- Handles missing or malformed data:
if (!cleanJsonData || !cleanJsonData.basics) { context.res = { status: 404, body: `Data not found or missing 'basics' field for id: ${id}`, }; return; }
- Logs errors and returns appropriate HTTP status codes.
Detailed logs are provided to aid in debugging:
- Logs the Cosmos DB endpoint and query details.
- Logs the fetched data and cleaned data.
-
Deploy the Azure Function:
func azure functionapp publish <function-app-name>
-
Ensure the required environment variables are set in the Azure Function App settings.