Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用fastify&;发送文件;内斯特斯?_Javascript_Node.js_Typescript_Nestjs_Fastify - Fatal编程技术网

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