Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ This repository contains a Azure Functions HTTP trigger quickstart written in Ja
3) [Azure Developer CLI (AZD)](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd)
4) [Visual Studio Code](https://code.visualstudio.com/) - Only required if using VS Code to run locally

### Get repo on your local machine
Run the following GIT command to clone this repository to your local machine.
```bash
git clone https://github.com/Azure-Samples/functions-quickstart-javascript-azd.git
```

## Run on your local environment

### Prepare your local environment
1) Add a file named local.settings.json file to the root of the project with the following contents. This will allow you to run your function locally.
1) Create a file named `local.settings.json` and add the following:
```json
{
"IsEncrypted": false,
Expand Down Expand Up @@ -62,6 +68,61 @@ curl -i -X POST http://localhost:7071/api/httppostbodyfunction -H "Content-Type:
5) Open the file src/functions/test/ which contains a GET and POST test
6) Click the "Send Request" link for each and see the results in the right-hand pane that opens

## Source Code

The key code that makes tthese functions work is in `./src/functions/httpGetFunction.cs` and `.src/functions/httpPostBodyFunction.cs`. The function is identified as an Azure Function by use of the `@azure/functions` library. This code shows a HTTP GET triggered function.

```javascript
const { app } = require('@azure/functions');

app.http('httpGetFunction', {
methods: ['GET'],
authLevel: 'function',
handler: async (request, context) => {
context.log(`Http function processed request for url "${request.url}"`);

const name = request.query.get('name') || await request.text() || 'world';

return { body: `Hello, ${name}!` };
}
});
```
This code shows a HTTP POST triggered function which expects `person` object with `name` and `age` values in the request.

```javascript
const { app } = require('@azure/functions');

app.http('httpPostBodyFunction', {
methods: ['POST'],
authLevel: 'function',
handler: async (request, context) => {
context.log(`Http function processed request for url "${request.url}"`);

try {
const person = await request.json();
const { name, age } = person;

if (!name || !age) {
return {
status: 400,
body: 'Please provide both name and age in the request body.'
};
}

return {
status: 200,
body: `Hello, ${name}! You are ${age} years old.`
};
} catch (error) {
return {
status: 400,
body: 'Invalid request body. Please provide a valid JSON object with name and age.'
};
}
}
});
```

## Deploy to Azure

To provision the dependent resources and deploy the Function app run the following command:
Expand Down
1 change: 0 additions & 1 deletion infra/app/processor.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ module processor '../core/host/functions-flexconsumption.bicep' = {
virtualNetworkSubnetId: virtualNetworkSubnetId
instanceMemoryMB: instanceMemoryMB
maximumInstanceCount: maximumInstanceCount
identityClientId: identityClientId
}
}

Expand Down
2 changes: 1 addition & 1 deletion infra/app/vnet.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ resource virtualNetwork 'Microsoft.Network/virtualNetworks@2023-05-01' = {
delegations: [
{
name: 'delegation'
id: '${resourceId('Microsoft.Network/virtualNetworks/subnets', vNetName, 'app')}/delegations/delegation'
id: resourceId('Microsoft.Network/virtualNetworks/subnets/delegations', vNetName, 'app', 'delegation')
properties: {
//Microsoft.App/environments is the correct delegation for Flex Consumption VNet integration
serviceName: 'Microsoft.App/environments'
Expand Down