Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send file with fastify & nestjs?

I have the following front end middleware:

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'));
        }
    }
} 

It works fine with express, but with fastify, res.sendFile is undefined, so how can I fix that?

like image 514
yantrab Avatar asked Dec 08 '22 12:12

yantrab


1 Answers

Have a look at this issue. sendFile has no equivalent method in fastify; you have to do it manually:

const stream = fs.createReadStream(resolvePath('index.html'))
res.type('text/html').send(stream)
like image 81
Kim Kern Avatar answered Dec 10 '22 00:12

Kim Kern