John Grayson
John Grayson

Reputation: 1496

Missing s3 key error in static site served by node.js

I compiled and pushed my vue app onto S3 and I am serving it in my node/express app using s3-proxy. I can navigate to my app at / just fine. However, if I navigate directly to any other route such as /login, I get an Missing S3 key error. I understand that the root cause is there's no file in my bucket called login, but I'm not quite sure how to get around the issue. Here's my code:

app.get(
      "/*",
      s3Proxy({
      bucket: config.AWS.S3.UI.BUCKET,
      accessKeyId: config.AWS.ACCESS_KEY_ID,
      secretAccessKey: config.AWS.SECRET_ACCESS_KEY,
      defaultKey: "index.html",
    })
  );

Any advice would greatly be appreciated!

Upvotes: 0

Views: 204

Answers (1)

yurish
yurish

Reputation: 919

Use a middleware:

app.get(
    "/*",
    (req, res, next) => {
        req.url = "/"
        next()
    },
    s3Proxy({
        bucket: config.AWS.S3.UI.BUCKET,
        accessKeyId: config.AWS.ACCESS_KEY_ID,
        secretAccessKey: config.AWS.SECRET_ACCESS_KEY,
        defaultKey: "index.html",
    })
);

s3Proxy() creates a middleware, which is just a function with 3 arguments. You can create your own middlewares modifying the request. Changing req.url won't redirect the client to the root page, the vue application will be able to handle the routing as intended.

Note that any other middleware you were to put after s3Proxy will receive modified requests too.

Upvotes: 1

Related Questions