title | description | author | ms.author | ms.reviewer | ms.custom | ms.date | ms.service | ms.subservice | ms.topic | monikerRange |
---|---|---|---|---|---|---|---|---|---|---|
Connect to and query using Node.js and mssql npm package |
Learn how to connect to a database in Azure SQL Database and query data using Node.js and mssql npm package. |
diberry |
diberry |
mathoma |
passwordless-js |
03/04/2025 |
azure-sql-database |
security |
quickstart |
= azuresql || = azuresql-db |
[!INCLUDEappliesto-sqldb]
This quickstart describes how to connect an application to a database in Azure SQL Database and perform queries using Node.js and mssql. This quickstart follows the recommended passwordless approach to connect to the database.
Passwordless connections offer a more secure mechanism for accessing Azure resources. The following high-level steps are used to connect to Azure SQL Database using passwordless connections in this article:
- Prepare your environment for password-free authentication.
- For a local environment: Your personal identity is used. This identity can be pulled from an IDE, CLI, or other local development tools.
- For a cloud environment: A managed identity is used.
- Authenticate in the environment using the
DefaultAzureCredential
from the Azure Identity library to obtain a verified credential. - Use the verified credential to create Azure SDK client objects for resource access.
You can learn more about passwordless connections on the passwordless hub.
- An Azure subscription
- A database in Azure SQL Database configured for authentication with Microsoft Entra ID (formerly Azure Active Directory). You can create one using the Create database quickstart.
- Bash-enabled shell
- Node.js LTS
- Visual Studio Code
- Visual Studio Code App Service extension
- The latest version of the Azure CLI
[!INCLUDE passwordless-configure-server-networking]
The steps in this section create a Node.js REST API.
-
Create a new directory for the project and navigate into it.
-
Initialize the project by running the following command in the terminal:
npm init -y
-
Install the required packages used in the sample code in this article:
npm install mssql express swagger-ui-express yamljs dotenv
-
Open the project in Visual Studio Code.
code .
-
Open the
package.json
file and add the following property and value after the name property to configure the project for ESM modules."type": "module",
To create the Express.js OpenAPI application, you'll create several files:
File | Description |
---|---|
.env.development |
Local development-only environment file. |
index.js |
Main application file, which starts the Express.js app on port 3000. |
person.js |
Express.js /person route API file to handle CRUD operations. |
openapi.js |
Express.js /api-docs route for OpenAPI explorer UI. Root redirects to this route. |
openApiSchema.yml |
OpenAPI 3.0 schema file defining Person API. |
config.js |
Configuration file to read environment variables and construct appropriate mssql connection object. |
database.js |
Database class to handle Azure SQL CRUD operations using the mssql npm package. |
./vscode/settings.json |
Ignore files by glob pattern during deployment. |
-
Create an
index.js
file and add the following code::::code language="javascript" source="~/../azure-typescript-e2e-apps/quickstarts/azure-sql/connect-and-query/js/index.js":::
-
Create a
person.js
route file and add the following code::::code language="javascript" source="~/../azure-typescript-e2e-apps/quickstarts/azure-sql/connect-and-query/js/person.js" :::
For passwordless authentication, change the param passed into
createDatabaseConnection
fromSQLAuthentication
toPasswordlessConfig
.const database = await createDatabaseConnection(PasswordlessConfig);
-
Create an
openapi.js
route file and add the following code for the OpenAPI UI explorer::::code language="javascript" source="~/../azure-typescript-e2e-apps/quickstarts/azure-sql/connect-and-query/js/openapi.js":::
-
Create an
openApiSchema.yml
file and add the following code so the OpenAPI UI explorer knows what APIs and models to display::::code language="yml" source="~/../azure-typescript-e2e-apps/quickstarts/azure-sql/connect-and-query/ts/src/openApiSchema.yml":::
The mssql package implements the connection to Azure SQL Database by providing a configuration setting for an authentication type.
-
In Visual Studio Code, create a
config.js
file and add the following mssql configuration code to authenticate to Azure SQL Database.:::code language="javascript" source="~/../azure-typescript-e2e-apps/quickstarts/azure-sql/connect-and-query/js/config.js":::
Create a .env.development
file for your local environment variables
Add the following text and update with your values for <YOURSERVERNAME>
and <YOURDATABASENAME>
.
AZURE_SQL_SERVER=<YOURSERVERNAME>.database.windows.net
AZURE_SQL_DATABASE=<YOURDATABASENAME>
AZURE_SQL_PORT=1433
AZURE_SQL_AUTHENTICATIONTYPE=azure-active-directory-default
Note
Passwordless configuration objects are safe to commit to source control, since they do not contain any secrets such as usernames, passwords, or access keys.
Add the following text and update with your values for <YOURSERVERNAME>
, <YOURDATABASENAME>
, <YOURUSERNAME>
, and <YOURPASSWORD>
.
AZURE_SQL_SERVER=<YOURSERVERNAME>.database.windows.net
AZURE_SQL_DATABASE=<YOURDATABASENAME>
AZURE_SQL_PORT=1433
AZURE_SQL_USER=<YOURUSERNAME>
AZURE_SQL_PASSWORD=<YOURPASSWORD>
Warning
Use caution when managing connection objects that contain secrets such as usernames, passwords, or access keys. These secrets shouldn't be committed to source control or placed in unsecure locations where they might be accessed by unintended users.
-
Create a
database.js
file and add the following code::::code language="javascript" source="~/../azure-typescript-e2e-apps/quickstarts/azure-sql/connect-and-query/js/database.js":::
The app is ready to be tested locally. Make sure you're signed in to the Azure Cloud in Visual Studio Code with the same account you set as the admin for your database.
-
Run the application with the following command. The app starts on port 3000.
NODE_ENV=development node index.js
The Person table is created in the database when you run this application.
-
In a browser, navigate to the OpenAPI explorer at http://localhost:3000.
-
On the Swagger UI page, expand the POST method and select Try it.
-
Modify the sample JSON to include values for the properties. The ID property is ignored.
:::image type="content" source="media/passwordless-connections/api-testing-javascript.png" lightbox="media/passwordless-connections/api-testing-javascript.png" alt-text="A screenshot showing how to test the API.":::
-
Select Execute to add a new record to the database. The API returns a successful response.
-
Expand the GET method on the Swagger UI page and select Try it. Select Execute, and the person you just created is returned.
-
Create a
.vscode
folder and create asettings.json
file in the folder. -
Add the following to ignore environment variables and dependencies during the zip deployment.
{ "appService.zipIgnorePattern": ["./.env*","node_modules{,/**}"] }
The app is ready to be deployed to Azure. Visual Studio Code can create an Azure App Service and deploy your application in a single workflow.
-
Make sure the app is stopped.
-
Sign in to Azure, if you haven't already, by selecting the Azure: Sign In to Azure Cloud command in the Command Palette (Ctrl + Shift + P)
-
In Visual Studio Code's Azure Explorer window, right-click on the App Services node and select Create New Web App (Advanced).
-
Use the following table to create the App Service:
Prompt Value Enter a globally unique name for the new web app. Enter a prompt such as azure-sql-passwordless
. Post-pend a unique string such as123
.Select a resource group for new resources. Select +Create a new resource group then select the default name. Select a runtime stack. Select an LTS version of the Node.js stack. Select an OS. Select Linux. Select a location for new resources. Select a location close to you. Select a Linux App Service plan. Select Create new App Service plan. then select the default name. Select a pricing tier. Select Free (F1). Select an Application Insights resource for your app. Select Skip for now. -
Wait until the notification that your app was created before continuing.
-
In the Azure Explorer, expand the App Services node and right-click your new app.
-
Select Deploy to Web App.
:::image type="content" source="media/passwordless-connections/visual-studio-code-web-app-deploy.png" alt-text="Screenshot of Visual Studio Code in the Azure explorer with the Deploy to Web App highlighted.":::
-
Select the root folder of the JavaScript project.
-
When the Visual Studio Code pop-up appears, select Deploy.
When the deployment finishes, the app doesn't work correctly on Azure. You still need to configure the secure connection between the App Service and the SQL database to retrieve your data.
[!INCLUDE passwordless-connect-azure-sql]
[!INCLUDE password-connect-azure-sql]
Browse to the URL of the app to test that the connection to Azure SQL Database is working. You can locate the URL of your app on the App Service overview page.
The person you created locally should display in the browser. Congratulations! Your application is now connected to Azure SQL Database in both local and hosted environments.
Tip
If you receive a 500 Internal Server error while testing, it may be due to your database networking configurations. Verify that your logical server is configured with the settings outlined in the Configure the database section.
[!INCLUDE passwordless-resource-cleanup]
The sample code for this application is available: