Skip to content
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

Closed
mirabledictu opened this issue Sep 3, 2021 · 7 comments · Fixed by #480
Closed

support for serving static files #31

mirabledictu opened this issue Sep 3, 2021 · 7 comments · Fixed by #480
Assignees
Labels
discussion enhancement New feature or request

Comments

@mirabledictu
Copy link

mirabledictu commented Sep 3, 2021

Hi, is there an equivalent of

app.use(express.static(`${root}/dist/client`, { index: false }))

in h3? Thanks

@danielroe danielroe changed the title Equivalent of of express.static support for serving static files Sep 3, 2021
@mvrlin
Copy link

mvrlin commented Nov 6, 2021

@mirabledictu
You can use the serve-static package.

import serveStatic from 'serve-static'

// Available from /
app.use(serveStatic(`${root}/dist/client`))

// Available from /prefix
app.use('/prefix', serveStatic(`${root}/dist/client`))

@steinathan
Copy link

@mvrlin
Unfortunately, its 2022 & still not working

@pi0
Copy link
Member

pi0 commented Nov 15, 2022

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).

@NozomuIkuta NozomuIkuta added the enhancement New feature or request label Dec 12, 2022
@pi0 pi0 closed this as completed in #480 Aug 1, 2023
@magne4000
Copy link

For people stumbling upon this issue, here is the current way to leverage serve-static:

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`)))

@imcm7
Copy link

imcm7 commented Dec 4, 2023

app.use(fromNodeMiddleware(serveStatic(`${root}/dist/client`)))

@magne4000 something not work, i try with nitroApp.h3App.use()

@gulshan
Copy link

gulshan commented Jan 2, 2024

I was able to use the built-in serveStatic like this-

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)

@IlyaSemenov
Copy link

app.use doesn't work indeed, but luckily there is a workaround:

import serveStatic from "serve-static"

export default defineNitroPlugin((nitroApp) => {
  nitroApp.h3App.stack.unshift({
    route: "/uploads",
    handler: fromNodeMiddleware(serveStatic("./uploads", { immutable: true })),
  })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants