Introduction: Azure Functions is a serverless compute service that enables you to run event-triggered code without explicitly provisioning or managing infrastructure. In this tutorial, we'll explore how to create and deploy Azure Functions using Node.js.
Prerequisites:
- Azure account (create one if you don't have it)
- Node.js installed
- Azure Functions Core Tools
(npm install -g azure-functions-core-tools)
Create an Azure Functions Project:
Open a terminal and run:
func init MyFunctionApp --language javascript
cd MyFunctionApp
Create a new function:
func new --name MyFunction --template "HTTP trigger"
Install dependencies:
Write your Function:
Open MyFunction/index.js
and replace the existing code with a simple example:
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const name = (req.query.name || (req.body && req.body.name));
const responseMessage = name
? `Hello, ${name}. This HTTP triggered function executed successfully.`
: "Please pass a name on the query string or in the request body.";
context.res = {
// status: 200, /* Defaults to 200 */
body: responseMessage,
};
};
Run and Test Locally:
Execute the following command to run your function locally:func start
Visit http://localhost:7071/api/MyFunction?name=John
in your browser or use a tool like curl
to test.
Deploy to Azure:
- Create a new function app in the Azure Portal.
- Update
MyFunctionApp/local.settings.json
with your Azure Functions app settings. - Deploy your function
func azure functionapp publish MyFunctionApp