This project demonstrates a simple Node.js application that connects to a Microsoft SQL Server database and retrieves data using the Express framework and mssql package.
Features:
- Connects to a SQL Server database using environment variables for secure configuration.
- Defines a route (
/data
) to retrieve data from a specific table using a GET request. - Parses JSON data in request body (if applicable).
- Handles errors during database connection and data fetching.
- Returns retrieved data as JSON.
Requirements:
- Node.js and npm installed (https://nodejs.org/en)
- Microsoft SQL Server database
mssql
package (npm install mssql
)express
package (npm install express
)dotenv
package (npm install dotenv
) (optional, for environment variables)
Setup:
-
Create a
.env
file (optional) and define the following environment variables:DB_USER
: Username for SQL Server accessDB_PASSWORD
: Password for SQL Server accessDB_SERVER
: Server name or IP address of your SQL Server instanceDB_DATABASE
: Database name to connect toPORT1
: Port number for the Node.js server to listen onPORT2
(optional): Port number for SQL Server (default: 1433)ENCRYPT
(optional): Set to "true" for encrypted connection
-
Replace
tableName01
in the code with the actual name of your table in the SQL Server database. -
Run the application using
node server.js
.
Deployment:
This is a basic example and might require adjustments for deployment depending on your environment. Consider using process managers like PM2 for production deployments.
Further Customization:
- Define additional routes for different functionalities.
- Implement more complex SQL queries for specific data retrieval needs.
- Enhance error handling to provide more specific messages.
- Integrate authentication and authorization mechanisms for secure access control.