I am using the following code to connect to my SQL Azure database using node.js
I took the code from How to connect an existing nodejs server app to Azure SQL database
const express=require('express');
const router = express.Router()
const sql = require('mssql')
const config = {
user: "<user>",
password: "<password>",
server: "<myserver>.database.windows.net",
database: "<mydatabase>",
connectionTimeout: 3000,
parseJSON: true,
options: {
encrypt: true,
enableArithAbort: true
},
pool: {
min: 0,
idleTimeoutMillis: 3000
}
};
const pool = new sql.ConnectionPool(config);
const poolConnect = pool.connect();
router.get('/', async function (req, res) {
console.log('here');
await poolConnect;
try {
const request = pool.request();
const result = await request.query('select 1 as number')
console.log(result);
res.json(result.recordset);
} catch (err) {
console.error('SQL error', err);
res.send(err);
}
});
I know the code is connecting, because if I change the password then I get an error like this
(node:24172) UnhandledPromiseRejectionWarning: ConnectionError: Login failed for user '<user>'.
at Connection.<anonymous> (C:\Users\myuser\node_modules\mssql\lib\tedious\connection-pool.js:68:17)
at Object.onceWrapper (events.js:417:26)
at Connection.emit (events.js:310:20)
at Connection.message (C:\Users\myuser\node_modules\mssql\node_modules\tedious\lib\connection.js:2148:18)
at Connection.dispatchEvent (C:\Users\myuser\node_modules\mssql\node_modules\tedious\lib\connection.js:1279:15)
But if I put the correct password in, then the code just does not output anything at all.
It appears that it is simply not entering the asynchronous function at router.get('/', async function (req, res) {
Any ideas why?
console.log('here')? If not, it's not a problem of azure or the db but a problem of your express app ... How do you start your listener and how do you do the request tohttp://yourhost/?