OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Teleturbis
  • Start date Start date
T

Teleturbis

Guest
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:

Code:
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:

Code:
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

Code:
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

<p>I have an ExpressJS application hosted on AWS Elastic Beanstalk that provides a single route <code>/minify</code>. This route compresses and minifies submitted files and sends them back as a response. Additionally, there is an optional query <code>save=1</code> which also saves the file on AWS S3.</p>
<p>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.</p>
<p>Here is the relevant part of my code:</p>
<p><strong>minifyController.ts:</strong></p>
<pre class="lang-js prettyprint-override"><code>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 });
}
}
</code></pre>
<p><strong>aws.ts:</strong></p>
<pre class="lang-js prettyprint-override"><code>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}`);
}
}
</code></pre>
<p><strong>index.ts</strong></p>
<pre class="lang-js prettyprint-override"><code>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);
</code></pre>
<p>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?</p>
<p><strong>What I have already tried:</strong></p>
<ul>
<li>Commenting out the S3 function: this leads to everything working correctly.</li>
<li>Creating a separate route specifically for saving on S3: the problem persists.</li>
<li>Defined port 5000</li>
</ul>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top