Comments (9)
decided this was the wrong solution
@coreyfarrell please moderate your tone, an aggressive behavior is not welcomed.
from fastify-static.
This is a bug.
Nope, this is the correct behavior of the API, you are adding multiple times the same decorator.
I'll release very soon the new version of Fastify, which will have hasReplyDecorator
, so we can check it directly inside fastify-static
if a specific decorator has already been added.
In any case, the main issue will be that a user will get an "unpredictable" behavior from reply.sendFile
because it will work only for the first registered static plugin.
The best solution would be register the two instances of fastify-static
in two different plugins (which is the main idea behind the Fastify's plugin architecture), in this way there will be no conflicts.
// first plugin
fastify.register((instance, opts, next) => {
instance.register(require('fastify-static'), {
root: path.join(__dirname, 'node_modules'),
prefix: '/node_modules/'
})
// here `reply.sendFile` refers to 'node_modules' files
next()
})
// second plugin
fastify.register((instance, opts, next) => {
instance.register(require('fastify-static'), {
root: path.join(__dirname, 'html/myapp'),
prefix: '/myapp/'
})
// here `reply.sendFile` refers to 'html/myapp' files
next()
})
from fastify-static.
If there is time, it’d be nice to have the official solution documented in the readme: I don’t understand how and where one can do a reply.sendFile()
that considers both example directories.
I’d also find it useful if it were possible to merge multiple local directories into the same online directory. Local directories that are mentioned later would provide fallbacks for earlier local directories.
from fastify-static.
@rauschma would you mind to open an issue about this "merging" functionality? I don't know see how that could work.
from fastify-static.
Nice catch! Thanks!
This is a bug. Would you mind to send a PR?
from fastify-static.
I've created an option with docs and a test to ensure this can be dealt with, I don't think it can be fixed another way.
from fastify-static.
from fastify-static.
#63 adds an option to disable the reply decorator per instance of fastify-static
. This allows fastify-static
to be added multiple times in the same plugin context but requires explicitly choosing no more than one to provide reply.sendFile
. I'm aware you can put each fastify-static
into a separate plugin context but then you don't get reply.sendFile
in the global context. I saw hasReplyDecorator
in the code, decided this was the wrong solution for this module since each reply.sendFile
can have different settings so the conflict must be resolved explicitly.
// first plugin
fastify.register((instance, opts, next) => {
instance.register(require('fastify-static'), {
root: path.join(__dirname, 'node_modules'),
prefix: '/node_modules/'
})
// here `reply.sendFile` refers to 'node_modules' files
next()
})
// second plugin
fastify.register((instance, opts, next) => {
instance.register(require('fastify-static'), {
root: path.join(__dirname, 'html/myapp'),
prefix: '/myapp/'
})
// here `reply.sendFile` refers to 'html/myapp' files
next()
})
// here `reply.sendFile` is undefined.
from fastify-static.
This information should be extracted from the document or README.
from fastify-static.
Related Issues (20)
- Multi-root + pre-compression causes infinite loop HOT 2
- When using "@fastify/static", it does not trigger "server.get('*')" when accessing the / path HOT 6
- Not working after update HOT 3
- Forking send to @fastify/send to avoid CVE warning
- setNotFoundHandler and @fastify/static context issue HOT 2
- Parameter in route and serving files HOT 2
- Support URL for root parameter (and `file:` protocol) HOT 9
- Serve the same html for all get routes HOT 1
- Regression on isUtf8MimeType HOT 5
- Investigate path-scurry by @isaacs HOT 2
- Why @[email protected] bind [email protected] rather than 4.x HOT 1
- 我正在使用 nestjs,并通过 fastify-static 使用 public static,但我无法在打包时自动让目录 public 在目录 dist 内生成。 HOT 1
- . HOT 1
- inflight - transient dependency introduced memory leak HOT 8
- Correct module exports? HOT 2
- abstract the plugin to provide support of virtual file system HOT 1
- `FST_ERR_REP_ALREADY_SENT` when wrapping route handler HOT 5
- 404 when using wildcard HOT 3
- setting content-length HOT 5
- Is there a way to reduce the size of importing @fastify/static? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify-static.