【发布时间】:2023-03-02 21:41:01
【问题描述】:
通过 Hapi 将 MongoDB 查询响应流式传输到客户端的最佳方法是什么?我见过一些使用 http 或 request 的例子,但不是 hapi。
问题是我在客户端获得了连接和字符串化的 JSON 对象,但我不能对结果调用 JSON.parse,因为它不是有效的 JSON。
我见过的一些解决方案建议在发送到客户端之前在服务器端进行连接,但这似乎破坏了流的价值。
例如:
const Hapi = require('hapi'),
MongoClient = require('mongodb').MongoClient,
Readable = require('stream').Readable;
// Connection url
const url = 'mongodb://localhost:27017/test';
// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 8000
});
// Add the route
server.route({
method: 'GET',
path: '/stream',
handler: function (request, reply) {
let docs = [{ a: 1, b: 1 }, { a: 2, b: 2 }, { a: 3, b: 3 }, { a: 4, b: 4 }];
// Connect using MongoClient
MongoClient.connect(url, (err, db) => {
// Create a collection we want to drop later
const col = db.collection('stream_example');
// Insert documents into collection
col.insertMany(docs, { w: 1 }, function (err) {
if (err) return console.log(err);
// Peform a find to get a cursor
const stream = col.find()
.stream({
transform: function (doc) {
return JSON.stringify(doc);
}
});
reply(new Readable().wrap(stream));
});
});
}
});
// Start the server
server.start(err => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
返回一个 response.result 的:
"{"_id":"57b0b99d681bb97a9321f03e","a":1,"b":1}{"_id":"57b0b99d681bb97a9321f03f","a":2,"b":2}{"_id" :"57b0b99d681bb97a9321f040","a":3,"b":3}{"_id":"57b0b99d681bb97a9321f041","a":4,"b":4}"
这不是有效的 JSON,无法解析。
我尝试将此流通过管道传输到事件流模块的 .join('\n') 流中以添加换行符,同时还在构建字符串化 JSON 数组之前和之后推送字符串“[”和“]” ,但还没有成功。无论如何,这感觉太hacky了。
有没有更好的办法?
【问题讨论】:
标签: json node.js mongodb stream hapijs