A Step-by-Step Tutorial on Connecting Express.js to a Microsoft SQL Server

Posted by

In this tutorial, we will learn how to connect Express.js, a popular web framework for Node.js, to a Microsoft SQL Server database. This will allow you to build powerful web applications that can read and write data from a SQL Server database using Express.js.

Step 1: Install Required Dependencies

Before we can connect Express.js to a Microsoft SQL Server database, we need to install the necessary dependencies. Open your terminal and navigate to your project directory, then run the following command to install the required packages:

npm install express mssql

This will install Express.js and the mssql package, which is a Microsoft SQL Server client for Node.js.

Step 2: Create a SQL Server Database

Next, you need to create a SQL Server database that your Express.js application will connect to. You can create a new database using SQL Server Management Studio or any other SQL Server management tool.

Step 3: Set Up Connection Configuration

Now that we have our dependencies installed and our database created, we need to set up the connection configuration in our Express.js application. Create a new file called dbConfig.js in your project directory and add the following code:

const config = {
  user: 'your_username',
  password: 'your_password',
  server: 'localhost',
  database: 'your_database',
  options: {
    trustedConnection: true
  }
};

module.exports = config;

Replace your_username, your_password, and your_database with your SQL Server username, password, and database name.

Step 4: Connect Express.js to SQL Server

Now we can actually connect our Express.js application to the SQL Server database. Create a new file called app.js in your project directory and add the following code:

const express = require('express');
const app = express();
const sql = require('mssql');

const config = require('./dbConfig');

app.get('/', async (req, res) => {
  try {
    await sql.connect(config);
    const result = await sql.query`SELECT * FROM your_table`;
    res.json(result.recordset);
  } catch (err) {
    res.status(500).send(err);
  }
});

const PORT = 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

This code sets up a simple Express.js server that connects to the SQL Server database, executes a query to select all records from a table, and then returns the results as JSON. Replace your_table with the name of the table you want to query.

Step 5: Start the Express.js Server

To start your Express.js server and connect it to the SQL Server database, run the following command in your terminal:

node app.js

This will start the server on port 3000. You can now access your server at http://localhost:3000 and see the results of the SQL query.

Congratulations! You have successfully connected Express.js to a Microsoft SQL Server database. You can now build powerful web applications that interact with your SQL Server data using Express.js.

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@geminix5845
4 months ago

When creating a New Login in SQL Server, just under where you type the password are checkboxes. One of them says "User must change password on next login". That's why your passwords kept not working. Everytime you changed the password in MSSQL that box was checked. Then you need to set the Role as you did (though having a login with as few permissions as necessary is a good idea for production). Still liked your tutorial