I’m not having luck establishing a MS SQL connection.First time attempting to query MSSQL Sever from Node.js, I just want to try reading a table and dumping to a json file. To set this up, I took a connection string from a working .NET project, and parsed it into the config section of this a new node server.js script. I’m not seeing what’s wrong with the sql connection config but maybe I’m missing something. Any ideas of what I can try next in an effort to debug this would be appreciated. Code below:
Error: Login failed for user ‘web_preview’.
Error (encrypt: true): Failed to connect to WEBSRVR.xxx.com:1433 – self signed certificate
package.json
{
"dependencies": {
"mssql": "^10.0.2"
}
}
server.js
const fs = require('fs');
const sql = require('mssql');
const config = {
user: 'web_user',
password: 'web_password;TrustServerCertificate=True',
server: 'WEBSRVR.xxx.com',
database: 'reports',
encrypt: false // Disable SSL encryption
};
// SQL query to execute
const query = 'SELECT top 10 * FROM myReports';
(async () => {
try {
// Connect to the database
await sql.connect(config);
// Execute the query
const result = await sql.query(query);
// Save the result to a JSON file
const jsonResult = JSON.stringify(result.recordset, null, 2);
fs.writeFileSync('query_result.json', jsonResult);
console.log('Query result saved to query_result.json');
} catch (err) {
console.error('Error:', err.message);
} finally {
// Close the connection pool
await sql.close();
}
})();