-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support for serving static files #31
Comments
@mirabledictu import serveStatic from 'serve-static'
// Available from /
app.use(serveStatic(`${root}/dist/client`))
// Available from /prefix
app.use('/prefix', serveStatic(`${root}/dist/client`)) |
@mvrlin |
Please try using https://github.com/unjs/nitro for supporting static asset serving and many more benefits on top of h3. h3 aims to be a platform-independent framework and it is not easy to provide a solution that works out of the box without depending on node fs. Thinking about integration with unstorage for static asset support (pending for unjs/unstorage#21). |
For people stumbling upon this issue, here is the current way to leverage import { createApp, fromNodeMiddleware } from 'h3'
import serveStatic from 'serve-static'
const app = createApp()
// Available from /
app.use(fromNodeMiddleware(serveStatic(`${root}/dist/client`)))
// Available from /prefix
app.use('/prefix', fromNodeMiddleware(serveStatic(`${root}/dist/client`))) |
@magne4000 something not work, i try with nitroApp.h3App.use() |
I was able to use the built-in let staticHandler = h3.eventHandler(async (event) => {
let dir = import.meta.resolve('./static')
await h3.serveStatic(event, {
fallthrough: true,
indexNames: undefined,
getContents: (id) => readFile(new URL(dir + id)),
getMeta: async (id) => {
const stats = await stat(new URL(dir + id)).catch(console.warn);
if (!stats || !stats.isFile()) {
return;
}
return {
size: stats.size,
mtime: stats.mtimeMs,
};
},
});
})
app.use("/", staticHandler) |
import serveStatic from "serve-static"
export default defineNitroPlugin((nitroApp) => {
nitroApp.h3App.stack.unshift({
route: "/uploads",
handler: fromNodeMiddleware(serveStatic("./uploads", { immutable: true })),
})
}) |
Hi, is there an equivalent of
in h3? Thanks
The text was updated successfully, but these errors were encountered: