【发布时间】:2020-09-17 22:24:30
【问题描述】:
我在 NodeJS 中使用mssql 来获取大量行。
我想将结果通过管道传输到 Express 中的 HTTP 响应,但我不知道如何实现。
db.js
const sql = require('mssql');
const config = {
user: 'user',
password: 'pass',
server: 'host',
database: 'db',
};
index.js
我能够将结果流式传输到命令行:
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;
但是,使用 request.pipe() 会导致错误:
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 抛出新的错误。TypeError('ERR_INVALID_ARG_TYPE', '第一个参数', ^
TypeError [ERR_INVALID_ARG_TYPE]:第一个参数必须是 输入字符串或缓冲区 在 write_ (_http_outgoing.js:647:11) 在 ServerResponse.write (_http_outgoing.js:622:10) 在 Request.emit (events.js:180:13
【问题讨论】:
-
您正在使用两个请求,一个是对 sql 的请求,另一个是可能是 http req 对象。您可以将结果流式传输到控制台,因为请求对象会检查“行”事件,而您期望同一对象而不是“请求”对象上的“完成”事件。
-
更正我上面的评论:你在同一个对象上调用 'pipe' 方法而不是 'req' 对象
标签: sql-server node.js express