uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
7.95k stars 570 forks source link

How to serve a static file directory with uWS #997

Closed JemiloII closed 9 months ago

JemiloII commented 10 months ago

So for this next project I'm doing, I really want to use uWS for https and wss. Most of my projects have the two split unfortunately. This project is much simpler but I don't see how with uWS to serve static content. I don't want to have to make a wildcard and do file lookups. I just want a simple thing like express, where I just do .static(route). I have a couple image directories, audio directory, and an angular build dir I want to make static. Does uWS have a simple way of doing this?

uNetworkingAB commented 10 months ago

No but it could possibly be done at some point, maybe.

criejs commented 9 months ago

Why not using Nginx for static files?

nickchomey commented 9 months ago

Why not cloudflare cdn? They also have websockets support among plenty of other things

dalisoft commented 9 months ago

@JemiloII You can look at here for ways to serve file/directory. Or use Cloudflare CDN as suggested by @nickchomey

JemiloII commented 9 months ago

Can't use cloud flare as a cdn, my website generates images and other content. It would be too slow to upload to a cdn and then serve. Faster to just serve from the server.

I don't want to use ngnix to serve static files. I just want to use uWS. I don't want to mess with proxies, on a server instance just to serve content on port 80/443. For my experience with Cloudflare, it doesn't play nice with sockets on other ports if you're not a pro member. Even as a pro member, the sockets for my use cases when proxies perform better on those ports.

nickchomey commented 9 months ago

I think you're misunderstanding a cdn. If you proxy your dns through cloudflare, it will automatically cache all static assets when requested and serve those automatically to future visitors. Not only that, but it gets served from "the edge" - their 300+ datacenters that are much closer to your visitors than your server will be.

Html needs to be specifically selected for caching though - they have docs on it.

They also offer Cloudflare Pages for static sites. It might suit your needs.

uNetworkingAB commented 9 months ago

App.static(path) could definitely be a feature to look at some point but not right now. uWS doesn't exist where it doesn't provide exceptional value, and so if it would have app.static, it would have to be one of the best performing such features. Right now, that's not the case, since it would require kTLS and sendfile which would lock it to Linux only, which is not the plan. So it's more than just an opinion - I don't want to add features which aren't obviously motivated. Esp. not since most companies use proxies and proxies have static file serving built-in