popClingwrap
popClingwrap

Reputation: 4217

Can an express app handle requests made to another server

Total Http/Express noob here. Just messing around and getting stuck.

I have an app set up that is serving static pages from a public dir. I also have the following route:

app.get('*', (req, res, next)=>{
   console.log(req.url)
   next();
});

This logs all the requests that are being made on my app but when one of my static pages requests an image from a different source eg S3 or Google, then that doesn't get logged. Which makes sense as that request obviously isn't hitting any of my routes.

So is there a way that I can get express or node to be aware of those external request? Really I just need to see the URLs that assets are being requested on, similar to the Network tab in Chrome dev tools.

Upvotes: 0

Views: 214

Answers (1)

kevintechie
kevintechie

Reputation: 1521

You can do it by sending a 302 redirect from your server to the image in S3, etc. Generally, this is not a good practice because redirected requests take additional browser and network overhead. It's probably OK for a few images, but you likely don't want to do it for all.

You could also proxy the image request. In this case your server would request the image from S3 and then stream the image back to the client. This also has performance impacts on your server.

If you want to know what images were requested and how often, then using log analysis on S3 or other storage resource is probably a better way to go.

Upvotes: 1

Related Questions