Javascript 如何使用fastify&;发送文件;内斯特斯?
我有以下前端中间件:Javascript 如何使用fastify&;发送文件;内斯特斯?,javascript,node.js,typescript,nestjs,fastify,Javascript,Node.js,Typescript,Nestjs,Fastify,我有以下前端中间件: 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.
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
是未定义的
,所以我如何解决这个问题呢?看看这个sendFile
在fastfy中没有等效的方法;您必须手动执行此操作:
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)
哦,毕竟不适合你,或者你为什么不接受?reply.type未定义。你是否使用了
res.type
?