Skip to content

madnoiseless/Node.js-API-with-SQL-Server-and-Express

Repository files navigation

Node.js API with SQL Server and Express

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:

  1. Create a .env file (optional) and define the following environment variables:

    • DB_USER: Username for SQL Server access
    • DB_PASSWORD: Password for SQL Server access
    • DB_SERVER: Server name or IP address of your SQL Server instance
    • DB_DATABASE: Database name to connect to
    • PORT1: Port number for the Node.js server to listen on
    • PORT2 (optional): Port number for SQL Server (default: 1433)
    • ENCRYPT (optional): Set to "true" for encrypted connection
  2. Replace tableName01 in the code with the actual name of your table in the SQL Server database.

  3. 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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published