我的“快车”服务器需要向另一个服务机构提出替代要求,当我收到答复时,我正试图把双向反馈给客户,但答复是空的。 这里,我利用一个公开的公益物创建的榜样;斯塔尔·瓦尔斯·安森。
const fastify = require("fastify")({ logger: true });
// return JSON from SWAPI - works!
fastify.get("/api/json", async (request, reply) => {
const response = await fetch("https://swapi.dev/api/people/1/");
const json = await response.json();
return reply.send(json);
});
// return stream from SWAPI - doesn t work
// response is an empty object: {}
fastify.get("/api/stream", async (request, reply) => {
const response = await fetch("https://swapi.dev/api/people/1/");
return reply.send(response.body);
});
fastify.listen({ port: 3000 }, (err) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
});
Above is the entire server file, but you can also clone my GitHub repo if you want to:
https://github.com/TJBlackman/fastify-stream-test
Thanks in advanced for the help!