[英]How to send file with fastify & nestjs?
我有以下前端中间件:
export class FrontendMiddleware implements NestMiddleware {
use(req: any, res: any, next: () => void) {
const url = req.originalUrl;
if (url.indexOf('rest') === 1) {
next();
} else if (allowedExt.filter(ext => url.indexOf(ext) > 0).length > 0) {
res.sendFile(resolvePath(url));
} else {
res.sendFile(resolvePath('index.html'));
}
}
}
它在 express 上工作正常,但在 fastify 上, res.sendFile
是undefined
,那么我该如何解决呢?
看看这个问题。 sendFile
在 fastify 中没有对应的方法; 你必须手动完成:
const stream = fs.createReadStream(resolvePath('index.html'))
res.type('text/html').send(stream)
您还可以将 index.html 存储在内存中并从中发送:
const bufferIndexHtml = fs.readFileSync('index.html')
res.type('text/html').send(bufferIndexHtml)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.