#How to load images uploaded to the server

7 messages · Page 1 of 1 (latest)

safe willow
#

I have uploaded images to a folder named uploads on my server. I would like to access the images through a url e.g. http://localhost:3000/uploads/image.jpg. I have tried to configure it to work two different ways but have been unsuccessful.

  ServeStaticModule.forRoot({
    rootPath: join(__dirname, '..', 'uploads')
  })
  app.useStaticAssets(join(__dirname, '..', '/uploads'), {
    index: false,
    prefix: 'uploads'
  });

Is there something I may be missing?

humble juniper
#

Are you certain that join(__dirname, '..', '/uploads') points to where you think it does?

safe willow
#

@humble juniper Maybe it doesn't. Here is my project structure.

safe willow
humble juniper
#

So you probably need another '..' in there, or use join(process.cwd(), 'uploads') instead

safe willow
#

I just got it to work using a different method.

#
  {
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "deleteOutDir": true,
    "assets": [
      {
        "include": "../uploads",
        "outDir": "dist/uploads",
        "watchAssets": true
      }
    ],
    "watchAssets": true
  }
}
  app.useStaticAssets(join(__dirname, '..', 'uploads'), {
    prefix: '/uploads'
  });