'How I can use sendFile in fastify?

Server.ts

import fastify from "fastify";
import cookie from 'fastify-cookie';
import apiRoute from './routes/api';
import jwtPlugin from "./plugins/jwtPlugin";
import closePlugin from "./plugins/closePlugin";
import path from "path";

const PORT = parseInt(process.env.PORT!, 10)

export default class Server {
    app = fastify({ logger: true })

    constructor() {
        this.setup()
    }

    setup() {
        this.app.get('/', (request, reply) => {
            reply.send({ hello: 'world' })
        })
        this.app.register(apiRoute, { prefix: '/api' })
        this.app.register(cookie)
        this.app.register(require('fastify-url-data'))
        this.app.register(jwtPlugin)
        this.app.register(closePlugin)
        this.app.setErrorHandler((error, request, reply) => {
            reply.send({
                statusCode: error.statusCode,
                name: error.name,
                message: error.message,
                validation: error.validation,
                stack: error.stack,
            })
        })
        this.app.register(require('fastify-rate-limit'), {
            max: 100,
            timeWindow: '1 minute'
        })
        this.app.register(require('fastify-static'), {
            root: path.join(__dirname, 'public')
        })
    }

version/index.ts

const versionRoute: FastifyPluginCallback = (fastify, opts, done) => {

    //Todo 1. get version of app
    //Define request body to fastify
    fastify.post(
        //Route
        '/version_info',
        async (request, reply) => {

            try {
                const result: VersionBody[] = await Version.getVersionInfo("testServer")
                reply.send(result[0])
            } catch (error) {
                reply.status(500)
                reply.send({
                    code: 500,
                    error: "Version Error",
                    message: error
                })
            }
        }
    )

    //Todo 2. get update file
    //Define request body to fastify
    fastify.get('/update_file', function (req, reply) {
        reply.sendFile(...)
    })    
    
    done()
}

export default versionRoute

Hi, I have a question. I want to send the file, when request to specific url. So, I install fastify-specific and register.

But, it show error message like, 'FastifyReply<Server, IncomingMessage, ServerResponse, RouteGenericInterface, unknown>' type does not have property 'sendFile'

How I can register reply.sendFile in fastify?

or Is there any way to send file in fastify?

If you know about it, please help me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source