fastify / fastify/fastify-cli

Project generated with typescript support cannot serve static file using fastify-static plugin

Open
#600 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
733
Forks
177
Avg merge
1d 20h
Merged PRs (30d)
3

Description

### Prerequisites

- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the bug has not already been reported

### Fastify version

4.0.0

### Plugin version

5.7.1

### Node.js version

v19.3.0

### Operating system

Linux

### Operating system version (i.e. 20.04, 11.3, 10)

manjaro

### Description

I have generated a project with typescript support and installed `fastify-static` to serve `hello.html` file at the root url.

I am getting this error `Route GET:/hello.html not found` on log when trying to access the root on browser.
On browser I get `{"message":"Route GET:/hello.html not found","error":"Not Found","statusCode":404}`

I have checked the `/dist` directory and it doesn't have the `hello.html` file in it. I tried to delete the `dist` directory and then restart the server using `npm run dev` but it doesn't solve the issue.

But when I tried to create a project without typescript it works as expected.

`src` directory
```
src
├── app.ts
├── plugins
│   ├── README.md
│   ├── sensible.ts
│   └── support.ts
└── routes
├── example
│   └── index.ts
├── hello.html // file I want to serve in root url
├── README.md
└── root.ts
```

`dist` directory
```
dist
├── app.d.ts
├── app.js
├── app.js.map
├── plugins
│   ├── sensible.d.ts
│   ├── sensible.js
│   ├── sensible.js.map
│   ├── support.d.ts
│   ├── support.js
│   └── support.js.map
└── routes
├── example
├── root.d.ts
├── root.js
└── root.js.map
```

`root.ts`
```

import { FastifyPluginAsync } from "fastify";
import fastifyStatic from "@fastify/static";
import { join } from "path";

const root: FastifyPluginAsync = async (fastify, opts): Promise => {
fastify.register(fastifyStatic, {
root: join(__dirname, ""),
});

fastify.get("/", async function (request, reply) {
reply.sendFile("hello.html");
});
};

export default root;
```

## Project without typescript

Directory structure
```
.
├── app.js
├── package.json
├── package-lock.json
├── plugins
│   ├── README.md
│   ├── sensible.js
│   └── support.js
├── README.md
├── routes
│   ├── example
│   ├── hello.html // file I want to serve in root url
│   ├── README.md
│   └── root.js
└── test
├── helper.js
├── plugins
└── routes
```

`root.js`
```

"use strict";
const path = require("path");

module.exports = async function (fastify, opts) {
fastify.register(require("@fastify/static"), {
root: path.join(__dirname, ""),
});

fastify.get("/", async function (request, reply) {
reply.sendFile("hello.html");
});
};
```

Most likely I am making some mistake causing this issue but I want to be sure. Thanks.

### Steps to Reproduce

1. Generate project

typescript support

```
fastify generate . --lang=ts
```
without typescript

```
fastify generate .
```
2. install fastify/static `npm i @fastify/static`
3. Create `hello.html` file in `/src/routes/`
4. Make changes in `root.js` or `root.ts` in `/src/routes/`

### Expected Behavior

I want to serve `hello.html` in the root URL.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.