dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.72k stars 165 forks source link

Problem serving static files #199

Closed djalbat closed 3 years ago

djalbat commented 3 years ago

Hi!

I am pretty sure that this is not a problem with serverless-http but let me run it past you anyway. I have a simple site here:

https://djalbat.com/

The CSS is embedded so there is only the single HTML file to serve plus a route to return 204 for the favicon.ico request.

This page works mostly:

https://djalbat.com/logicSeminar/

The CSS and (very!) old JavaScript files are served but the PNG file is not.

Returning to the main site, these requests are also faulty:

https://djalbat.com/ACtAPiCM.pdf https://djalbat.com/image/lovely_s_shaped_slur.jpg

So images, PDFs and so on are not getting served.

The application's architecture is pretty straightfoward. Here is the app.js file...

"use strict";

const express = require("express");

const { configurationUtilities } = require("necessary");

const staticRouter = require("./router/static"),
      defaultRouter = require("./router/default");

const { rc } = configurationUtilities,
      { ENVIRONMENT_NAME } = process.env;

rc(ENVIRONMENT_NAME);

const app = express(); ///

app.use(staticRouter);

app.use(defaultRouter);

module.exports = app;

...and the lambda.js file which uses serverless-http:

"use strict";

const serverless = require("serverless-http"); ///

const app = require("./app");

module.exports.handler = serverless(app);

I am pretty sure that this is down to AWS not serving files above a certain size or from a very restricted range of MIME types, but would be very grateful if you could you confirm?

djalbat commented 3 years ago

Here are the routers, by the way:

"use strict";

const express = require("express");

const constants = require("../constants");

const { PUBLIC_DIRECTORY_PATH } = constants;

const staticRouter = express.static(PUBLIC_DIRECTORY_PATH);

module.exports = staticRouter;
"use strict";

const express = require("express");

const paths = require("../paths"),
      faviconHandler = require("../handler/favicon");

const { FAVICON_PATH } = paths;

const defaultRouter = express.Router();

defaultRouter.get(FAVICON_PATH, faviconHandler);

module.exports = defaultRouter;
dougmoscrop commented 3 years ago

What does your serverless.yml look like? What type of HTTP event are you using?

(note that ideally, you don't serve your static files through Lambda, just via CloudFront -> S3, because there's no point paying for the invocation and the execution just to copy bytes)

djalbat commented 3 years ago

Hello Doug,

many thanks for the reply and apologies for the slightly tardy response.

You are right, the problem is that I am trying to serve binary content by way of a lambda through an HTTP API gateway and this simply will not work. I will use CloudFront as you suggest, so I have some learning to do.

By the way, there is no serverless.yml file, I am just pushing my lambda, together with the API Gateway configuration, straight into AWS with Terraform. See here:

https://github.com/xomicloud/javascript-secure-application

And specifically:

https://github.com/xomicloud/javascript-secure-application/blob/master/main.tf

Anyway, I will get on with mugging up on CloudFront.

Many thanks and kind regards, James