AWS Beanstalk returns 404 as response, if I use query/S3-upload

I have an ExpressJS application hosted on AWS Elastic Beanstalk that provides a single route /minify. This route compresses and minifies submitted files and sends them back as a response. Additionally, there is an optional query save=1 which also saves the file on AWS S3.

The problem occurs when I use this query. Although the file is successfully saved on S3, I receive an HTTP 404 status as a response. If I comment out the function for saving on S3, everything works correctly, and I receive the expected response.

Here is the relevant part of my code:

minifyController.ts:

export async function minifyController(
  req: Request,
  res: Response,
): Promise<void> {
  const saveFileOnS3 =
    parseFloat((req.query.save as string | undefined) || "0") === 1;

  console.log("saveFileOnS3", saveFileOnS3);

  const multerReq = req as Request & {
    files: { [fieldname: string]: Express.Multer.File[] };
  };

  if (
    !multerReq ||
    !multerReq.files ||
    (!multerReq.files.configFile && !multerReq.files.templateFile)
  ) {
    res.status(400).send(ERROR_MESSAGES.missingFiles);
    return;
  }
  if (!multerReq.files.configFile) {
    res.status(400).send(ERROR_MESSAGES.missingConfigFile);
    return;
  }
  if (!multerReq.files.templateFile) {
    res.status(400).send(ERROR_MESSAGES.missingTemplateFile);
    return;
  }

  try {
    const configFileContent =
      multerReq.files.configFile[0].buffer.toString("utf-8");
    const templateFileContent =
      multerReq.files.templateFile[0].buffer.toString("utf-8");

    const combinedContent = `${configFileContent}n${templateFileContent}`;

    const minified = await minifyFile(combinedContent);

    if (minified.code === undefined)
      throw new Error(ERROR_MESSAGES.minificationFailed);

    const brotli = await compressFile(minified.code);

    if (saveFileOnS3) await AWSUpload(brotli);

    console.log("Minification and compression successful");

    if (!res.headersSent)
      res
        .status(200)
        .setHeader("Content-Type", "application/javascript")
        .send(brotli);
  } catch (error: any) {
    console.error("Error in minification or compression:", error);
    res.status(500).send({ error: error.message || error });
  }
}

aws.ts:

export async function AWSUpload(file: Buffer): Promise<void> {
  const AWS_COMPRESSED_FILES_BUCKET = process.env.AWS_COMPRESSED_FILES_BUCKET;

  if (!AWS_COMPRESSED_FILES_BUCKET || AWS_COMPRESSED_FILES_BUCKET === "")
    throw new Error("AWS_COMPRESSED_FILES_BUCKET is not defined");

  const params = {
    Bucket: AWS_COMPRESSED_FILES_BUCKET,
    Key: `${crypto.randomUUID()}.min.js`,
    Body: file,
    ContentType: "application/javascript",
    ContentEncoding: "br", // Brotli encoding
  };

  try {
    const command = new PutObjectCommand(params);

    const response = await S3Client.send(command);

    if (response.$metadata.httpStatusCode !== 200) {
      throw new Error(
        `Failed to upload file to AWS - ${response.$metadata.httpStatusCode}`
      );
    }
  } catch (error) {
    throw new Error(`Failed to upload file to AWS - ${error}`);
  }
}

index.ts

import app from "./config/init";

// The requestLogger middleware is only used in development
import { requestLogger } from "./middlewares/dev";
app.use(requestLogger);

// minify-Router
import minifyRouter from "./routes/minify";
app.use("/minify", minifyRouter);

I have found that the error does not occur when I define a separate route for the S3 version. What could be the reason that the 404 error only occurs when I try to save the file on S3?

What I have already tried:

  • Commenting out the S3 function: this leads to everything working correctly.
  • Creating a separate route specifically for saving on S3: the problem persists.
  • Defined port 5000