Within my Nest.js API, there is a GET endpoint that needs to retrieve a database row along with up to 6 image files (base64 encoded) in the response.
Currently, I am achieving this by extracting unique file names stored in 6 columns of the database and then using the @Res decorator to send one of the images back like so:
@Get('/findVehicleEntry/:id')
async findVehicleEntry(@Param('id') id: number, @Res() res) {
const resVehicle: Vehicle = await this.vehiclesService.findVehicleEntry(id);
if (resVehicle) {
res.sendFile(resVehicle.photo1, { root: 'image-uploads' });
}
}
However, my goal is to fetch each image from the folder, convert it to base64, assign it to the corresponding property in `resVehicle`, and eventually send all 6 images together in the response payload.
I envision the process to be something like this:
@Get('/findVehicleEntry/:id')
async findVehicleEntry(@Param('id') id: number, @Res() res) {
const resVehicle: Vehicle = await this.vehiclesService.findVehicleEntry(id);
if (resVehicle) {
let image = something.get('resVehicle.photo1', 'my/path/to/image-uploads');
image = Buffer.from(image).toString('base64');
resVehicle.photo1 = image;
// Repeat for remaining 5 images
res.send(resVehicle);
}
}
This project marks my introduction to Nest/Express/Node development, and being new to APIs, guidance on best practices or better approaches would be greatly appreciated. Thank you!
Edit: Recent learnings have highlighted concerns around encoding large files in base64. While willing to abandon this idea, I am still seeking advice on effectively returning both the JSON object from the database and the 6 associated images within the same response structure.