Repo Type | Languages | Products | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Sample |
|
|
Creating a bare minimum Azure Function REST API to return JSON results from Azure Database for PostgreSQL with Node.js |
Creating a serverless REST API with Azure Functions is just a matter of a few lines of code. Azure Functions is an outstanding modern serverless computing service that developers use to create scalable solutions without worrying about the infrastructure setup. Node.js is another most popular selections to develop scalable, refined, high-performance REST API to be used by diverse clients. Azure Functions takes care of running the Node.js code.
The code is triggered when Azure Functions receives an HTTP request. The function code handles the request, prepares a SQL query with the given parameters, executes the query against the PostgreSQL server, converts the results to JSON, and returns it to the caller.
This project was kick-started to create an endpoint to query and get JSON data from Azure Database for PostgreSQL for a handful of in-house applications.
- Connection Credentials fetched from environment varibles during exeution avoiding plain-text storage
- Rich documentation available for the used libraries
This app uses node-postgres (pg) library to connect and query the Azure Database for PostgreSQL. The official documentation can be found here: https://node-postgres.com/
When deployed to Azure (or running locally), an HTTP request triggers the app. The request may or may not have query variables to refine the query results. The app expects a country
query parameter to filter the results.
Following softwares and libraries should be installed on the local machine to run and test the app locally:
- Node.js
- pg
- .NET Core
- Visual Studio Code
- Azure Functions extension for Visual Studio Code
- Azure Functions Core Tools
Adjust the connection details and credentials in local.settings.json
to point and connect to a desired PostgreSQL instance.
If script execution is disabled on your machine, you might encounter the following error during execution
To resolve this issue, change the script execution policy to "Unrestricted". Run the following command in the PowerShell as admin
Set-ExecutionPolicy Unrestricted
Revert the execution policy
Set-ExecutionPolicy Restricted
A successful execution will reveal the local URL
Refer to the Microsoft Docs for assistance: https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-node#run-the-function-locally
This app is a bare minimum working sample and can be deployed to Azure directly from the VS Code.
Refer to Microsoft Docs for step-by-step instructions: https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-node#sign-in-to-azure
Read the relevant blog to understand how to develop this App using the Portal: https://medium.com/swlh/building-a-rest-api-for-azure-postgresql-using-azure-functions-with-node-js-e33f1d7a1f4e