I'm using mssql in NodeJS to fetch a large number of rows.
I'd like to pipe the results to a HTTP response in Express, but I can't see how to achieve this.
db.js
const sql = require('mssql');
const config = {
user: 'user',
password: 'pass',
server: 'host',
database: 'db',
};
index.js
I am able to stream results to the command line:
router.get('/', function (req, res, next) {
new sql.ConnectionPool(config).connect().then(pool => {
const request = new sql.Request(pool);
request.stream = true;
request.query(`SELECT col1, col2 FROM mytable`);
request.on('row', row => {
console.log(row);
});
});
});
module.exports = router;
However, using request.pipe() results in an error:
router.get('/', function (req, res, next) {
new sql.ConnectionPool(config).connect().then(pool => {
const request = new sql.Request(pool);
request.pipe(res);
request.query(`SELECT col1, col2 FROM mytable`);
request.on('finish', () => {
res.end();
});
});
});
_http_outgoing.js:647 throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'first argument', ^
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string or Buffer at write_ (_http_outgoing.js:647:11) at ServerResponse.write (_http_outgoing.js:622:10) at Request.emit (events.js:180:13