DustInTheSilence
DustInTheSilence

Reputation: 575

Cannot upload a file in nestJS when the network conditions are poor

I have implemented in a controller a route to upload files from axios. For that I use FileInterceptor. Everything works properly but as soon as I activate network throttling in the browser the uploader does not work. Here is the snippet:

@Post('/upload')
@UseInterceptors(
  FileInterceptor("file", { 
    storage: diskStorage({
      destination: './src/uploads',
      // editFileName is a function that changes the name of the file
      filename: editFileName,
    }), 
  })
)
uploadFile(@UploadedFile() file) {
  try {
    console.log(file)
    return 'ok';
  } catch(e) {
    console.log(e)
  }
}

This is the following error that I get:

POST http://localhost:3003/user/file/upload net::ERR_CONNECTION_RESET

It seems, it tries to connect twice to complete the process. First connection time is 60s and the second one also but after that it throws the above error.

I try many ways changing the timeout param but in none of them not success. The first one is changing the destination config param of diskStorage from string to function:

destination: (req, file, callback) => {
  console.log(file);
  // That output I see when it uploads successfully in a normal network condition (wifi) 
  // but it ignores the timer time. Usually is after 5 seconds.
  req.socket.setTimeout(30000, () => console.log('Socket ErrorTimeout'))
  req.setTimeout(5000, () => console.log('Error req timeout'))
  callback(null, './src/uploads')
}

Next one is, inside of bootstrap function on the main file adding the following snippet:

const server = await app.listen(3003);
server.on('connection', (socket) => {
 console.log('Connected!')
 socket.setTimeout(5*10*1000)
})

And finally adding another module in FileUploader module but also not success:

@Module({
  imports: [
    MulterModule.register({
      dest: './src/uploads'
    }),
    HttpModule.register({
      timeout: 90000,
      maxRedirects: 5,
    })
  ],
  controllers: [FilesController]
})
export class FilesModule {}

Upvotes: 0

Views: 1036

Answers (1)

ehsan mandegar
ehsan mandegar

Reputation: 352

I recommend to use minio, its so good to store files and very good documentation

Upvotes: 1

Related Questions