Serving static assets

default discord avatar
henrituan
last year
1 1

Hi, I would like to have an example about serving static folders in Payload. Theses files can be used for instance in admin.meta.favicon field in payload.config.ts.

Here is my project tree:
image

I tried using express.static in my server.ts as follow but this does not work:

app.use('/public', express.static('assets/public')); => give 404 on localhost:3000/public

app.use('/public', express.static(__dirname + '/assets/public')); => also give 404 localhost:3000/public

Is there any example/documentation about serving static files in Payload that I can take a look at ? Thanks a lots !

  • discord user avatar
    DanRibbens
    Payload Team
    last year

    This is really an express question more than to do with Payload.

    When you say you get a 404 from localhost:3000/public are you specifying the file too as in localhost:3000/public/favicon.ico? The expected result for hitting just the directory path is a 404 unless you have a handler set also.

    Assuming that you are requesting files it has to be a problem with how you are calling express.static. We use path.resolve(__dirname, './assets/public') rather than + to concat path names together.

    What happens if you try app.use('/public', express.static(path.resolve(__dirname, './assets/public'));?

    1 reply
  • default discord avatar
    henrituan
    last year

    Thanks a lots, (path.resolve() indeed solves the problem !

Open the post
Continue the discussion in GitHub
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.