A serverless microservice using Azure Functions and Node.js. The service exposes a basic REST API for CRUD operations.
This project serves as a fundament on which you can build upon to create your next awesome (serverless) application.
- Leverages the power of TypeScript
- Validates request input
- Handles exceptions gracefully
- Lightweight with little configuration
- Organized project structure
- Shipped with tests using Jest
Install the dependencies using NPM or Yarn:
$ npm install
To run your function app locally using Core Tools, run the following command:
$ npm start
Note: In order to run a Azure Function App locally, the azure-functions-core-tools
package needs to be installed. Refer to the official documentation, or follow one of the steps below:
-
For Windows (using NPM):
$ npm install -g azure-functions-core-tools
-
For MacOS (using HomeBrew):
$ brew tap azure/functions $ brew install azure-functions-core-tools
-
For Linux: See the docs. It's a bit more complex.
Create a production-ready build of JavaScript files from the TypeScript source files:
$ npm run build:production
Deploy to Azure using the publish
command:
$ func azure functionapp publish <APP_NAME>
|-- functions
|-- IndexProduct
|-- function.json
|-- index.ts
|-- CreateProduct
|-- GetProduct
|-- ShowProduct
|-- DeleteProduct
|-- lib
|-- Constants
|-- Controllers
|-- Errors
|-- Schemas
|-- Services
|-- Util
Each function lives in its own folder. The configuration is defined in function.json
and the handler logic in index.ts
.
See package.json
for all available commands.
Running npm start
is the equivalent of:
$ npm run build
$ npm run tsc
$ func start --script-root ./functions
Running npm test
starts Jest and runs the tests.
Consider the practices mentioned below when coding your application.
When writing Azure Functions in JavaScript, you should write code using the async
and await
keywords. Writing code using async and await instead of callbacks or .then
and .catch
with Promises helps avoid two common problems:
- Throwing uncaught exceptions that crash the Node.js process, potentially affecting the execution of other functions.
- Unexpected behavior, such as missing logs from
context.log
, caused by asynchronous calls that are not properly awaited.
Write your business logic so that it is separate from your FaaS provider (e.g., Azure Functions), to keep it provider-independent, reusable and more easily testable. Construct and inject dependencies instead of using them directly in your function, this way you can easily replace these services with mocks.
Functions should be stateless and idempotent if possible. You have no guarantee that the application state is persisted across multiple or different function calls or contexts.
Large, long-running functions can cause unexpected timeout issues. Whenever possible, refactor large functions into smaller function sets that work together and return responses fast. Processing time-consuming tasks can be deferred until a later time.
- Play with different triggers & bindings to leverage the power of Event Grid, Service Bus, Blob Storage, SendGrid and many more services.
- Read more about the Saga pattern and Durable Functions to allow for transactions and states.
- Check out the additional resources below.
- Node.js - JavaScript runtime built on Chrome's V8 JavaScript engine
- TypeScript - JavaScript that scales
- Class Validator - Validation using decorators
- Jest - Delightful JavaScript testing
- What is Serverless?
- Everything about Microservices
- Creating your first function
- Function Reference for Node.js
- Using TypeScript with Azure Functions
- Building Serverless Microservices
- Check out the Serverless Framework
- How to implement the Saga pattern
- Manage your Azure resourcing using the CLI
- Shifting your Node express APIs to serverless