tedious4 express never returns response

195 Views Asked by At

I'm using express4-tedious to build a SQL web API

This is my code

const express = require('express');     // OAuth2 needs a web server to call back to
const morgan = require('morgan');       // morgan is HTTP middleware and logging
const path = require('path');           // helps resolving file paths
const fs = require('fs');               // filesystem
// https://www.npmjs.com/package/express4-tedious
const tediousExpress = require('express4-tedious');

dbconfig=
{
    "server"  : "localhost",
    "userName": "uname",
    "password": "pwd",
    "options": { "encrypt": true, "database": "MyDB", "trustServerCertificate":false }
}



const app = express();

// add tediousExpress to the middleware chain
app.use(function (req, res, next) {
    req.sql = tediousExpress(dbconfig);
    next();
});

// Initialize variables.
const port = 3030;

// Configure morgan module to log all requests.
app.use(morgan('dev'));

/* commented out to simplify issue
// set up route for static files
app.use(express.static('webapp/static'));
app.use(express.static('webapp/js'));
app.use(express.static('webapp/library'));
*/

// Start the server.
app.listen(port);
console.log('Listening on port ' + port + '...');

// Set up a route for index.html.
app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/webapp/index.html'));
});

app.get('/sm', function (req, res) {
    
    console.log("handling system")

    res.setHeader("Content-Type", "application/json");
 
    req
    .sql("select * from Mytable for json path")
    .fail(function(ex, res) { 
        res.statusCode = 500;   
        res.write(ex.message);
        res.end();
    } )    
    .into(res, '{}');

    // This always shows [] 
    console.log(res.outputData);
    
    // B. If I don't put this line in, the web page gets stuck
    res.send(res.outputData);


});

I'm running this in VS Code. console shows this when I hit the URL

handling system
[]

My problems are mentioned in code comments

Problem A

res.outputData always shows [] even though I know the query returns JSON.

Problem B

If I don't put res.send(res.outputData); in the code, when I call the url

  • the web page just spins forever
  • I can see in chrome network monitor that my url has no response

Also,

  • It seems redundant to explicitly send res.outputData data as an argument
  • This code is just something I came up with from experimentation
  • None of the example code in the links below require this additional line

stackoverflow

github

I'm not familiar enough with node.js to troubleshoot. I feel perhaps there is something wrong inside the module that is not being shown to me.

When I step through and debug in VS Code and step through I can see a wierd error about page not found, but I think this may be a red herring.

This code is 80% cut'n'paste as I learn.

If there is a more reliable SQL web API library than express4-tedious, I'm happy to take suggestions.

There is example code here https://github.com/JocaPC/express4-tedious

Edit:

  • I added a .fail function but there is no change to the outcome
  • I put a nonsense server name in and it behaved the same. So it's seems like it is failing but not raising any error or giving any clues
1

There are 1 best solutions below

2
On

Should your

dbconfig=
{
    "server"  : "localhost",
    "userName": "uname",
    "password": "pwd",
    "options": { "encrypt": true, "database": "MyDB", "trustServerCertificate":false }
}

be

dbconfig=
{
    server  : "localhost",
    userName: "uname",
    password: "pwd",
    options: { encrypt: true, database: "MyDB", trustServerCertificate:false }
}