- Notifications
You must be signed in to change notification settings - Fork611
How to serve a static file directory with uWS#1003
-
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 |
BetaWas this translation helpful?Give feedback.
All reactions
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
Replies: 14 comments 32 replies
-
No but it could possibly be done at some point, maybe. |
BetaWas this translation helpful?Give feedback.
All reactions
😄 3
-
Why not using Nginx for static files? |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Why not cloudflare cdn? They also have websockets support among plenty of other things |
BetaWas this translation helpful?Give feedback.
All reactions
-
@JemiloII You can look athere for ways to serve file/directory. Or use Cloudflare CDN as suggested by@nickchomey |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
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. |
BetaWas this translation helpful?Give feedback.
All reactions
-
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. |
BetaWas this translation helpful?Give feedback.
All reactions
-
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 |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I thought that this project is used only by smaller teams or geek guys. Companies usually have teams and use nodejs because they don't care about tech they just want to have something "stable" without SPOF (sorry:0). |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
The whole point is why we can't use a proxy and why the file sending interface should be different. We check the user's access rights to this file, i.e. before sending the file, we make queries to the database and determine whether the user can receive this file or not, plus private caching headers and give 304 status if clients have the file in the local cache, plus the real path to the file is often much more complex and private than a string request. Thus, it is much more useful to have a method The effectiveness of this method is important only in Linux (production) in Windows and macOS this method does not have to work very quickly and efficiently, because these operating systems are not for production but for development Thanks. |
BetaWas this translation helpful?Give feedback.
All reactions
-
I agree, you can make App.static using parameter route and sendFile |
BetaWas this translation helpful?Give feedback.
All reactions
👍 2
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Just started looking at this library and made a minimal file handler that worked for my case. I pushed it to a repohere importfsfrom'node:fs/promises';importmimefrom'mime-types';constsendFile=(filePath,res)=>{res.onAborted(()=>{res.aborted=true;});console.log(`send file ->${filePath}`)fs.readFile(filePath).then(data=>{if(!res.aborted){res.cork(()=>{res.writeStatus('200')res.writeHeader('Content-Type',mime.lookup(filePath))res.end(data)})}}).catch(err=>{console.log(err);if(!res.aborted){res.cork(()=>{res.writeStatus('404')res.end()})}})}export{sendFile} |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
yeah should probably use a stream like in the examplehere, but seem to work for my use case (just a smaller js files) for testing locally (do you know the size limit?), but i agree you should probably use nginx or similar in production. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
if you have small files and they are immutable, then when the server starts, scan the folder with the files, save all the files in the local JS cache, and always give the respond from the JS cache, you don’t even need promises |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
good idea - the "local JS cache" is just a map in memory you define i guess |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
i added something like thathere :) |
BetaWas this translation helpful?Give feedback.
All reactions
-
for |
BetaWas this translation helpful?Give feedback.
All reactions
-
It would be nice if zero-copy support would be set. (Interesting read:https://lwn.net/Articles/726917/). Note: And everyone suggest to setup proxy - nginx or cloudflare is the way. I don't fully agree with this. Setting additional proxy will add code/complexity to infrastructure and things to maintain. For smaller.. monolith/one-person projects it will be a problem to maintain. Note2: All frameworks that are based on uWS are doing some manual work there (better or worse). It would be nice to have native support. Note3: Not sure how uWS is working internally but I treat it as a "hyperfast js server", adding super fast methods for files would just move uWS toward that goal. Ps. sorry for the English. |
BetaWas this translation helpful?Give feedback.
All reactions
-
If you don't use CDN and proxy servers right away during development (and don't use a DB cluster during development), then mind-blowing fucked up will happen in production if the traffic flow exceeds the server's capabilities. It will be hell. And this hell will happen in production, and each of your programmers will have a burning ass. Use a proxy/content server from the very beginning of development. Separate proxy/content server. It should route traffic to your main servers (or a single server in the beginning) and can be a static file server. Yes, this adds complexity, but it must be done. Of course, it would be nice if uWS also had the functions of an ultra-fast proxy/content server, reducing the stack of used libraries, but this... Is this really necessary for this library? It's hard to guess. May be. Maybe not. |
BetaWas this translation helpful?Give feedback.
All reactions
-
still you can always have cloudflare proxy, and setup it in 10 minutes from scratch. Though you wouldn't need to change you approach on server. |
BetaWas this translation helpful?Give feedback.
All reactions
-
uWS will probably get a cache at some point. Maybe as a paid feature (along with other features). With a cache, all file serving routes could be shitty but still fast in hot path |
BetaWas this translation helpful?Give feedback.
All reactions
👎 1
-
too soon to be paid. You need to get big players first. |
BetaWas this translation helpful?Give feedback.
All reactions
-
I'm kinda surprised this is still going. All I really was hoping to achieve was to put uws on small box and have it serve as my backend, sockets, and static file path with minimal setup/config so I use the same ssl ports without having to put uws on a different port or have to use some kind of reverse proxy. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Ok. Then use Express. It really is that simple. uWS does not have any plan to add app.static unless a strong motivation comes along. And that motivation is not going to be you, writing that you are surprised nothing happened. |
BetaWas this translation helpful?Give feedback.
All reactions
-
I already know you guys aren't going to do that. I'm just amused that this is still going on after you let us know that you guys aren't doing it. More like the thread should just be closed and we add an note in an FAQ about it. |
BetaWas this translation helpful?Give feedback.
All reactions
-
It could be done at some point, it's a valid idea. But it isn't exactly simple to do, if you want it well done. uWS does not add half-assed solutions. You need sendfile and kTLS to make it anywhere near the best. kTLS is a major feature. I hear you say; "but I don't care about the best solution I just want it to be simple", ok, well again then use Express. Express definitely is simpler than uWS. |
BetaWas this translation helpful?Give feedback.
All reactions
-
What restrictions generally exist for sending static content via res.end() and what are they related to, blocking? Is res.end() limited to kilobytes, tens of kilobytes, or strings up to a thousand characters long? |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Oops, that was a stupid question, I was able to cause backpressure, the problem was in my proxy server.
Memory tracking in GC environments is quite difficult. Maybe it's not uWS but a nodejs error, or maybe something is happening in v8, GC or something else. You probably need some special solutions for analyzing this, but what’s the point of all this, fuck it. |
BetaWas this translation helpful?Give feedback.
All reactions
👎 1
-
@Msfrhdsa That (your variant) function is not correct and will send corrupt data to end users. You haven't tested it enough to stress the case where tryEnd consumed part of the chunk. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Have you considered just asking ChatGPT if you don't understand? I asked it, and it clearly understands it: The Parameters
Process
Key Considerations
Overall, the function is a robust solution for streaming data over HTTP in a Node.js environment, ensuring efficient data transfer and handling potential issues like backpressure and client disconnects. |
BetaWas this translation helpful?Give feedback.
All reactions
-
No, it isn't. They certainly can be different, and will be different in production stress. |
BetaWas this translation helpful?Give feedback.
All reactions
-
ChatGPT telling you why (you should really ask ChatGPT instead of having confident outbursts here) Why Offsets Are Used
Why It Might Seem RedundantIt might seem that using both
In summary, the use of offsets in the |
BetaWas this translation helpful?Give feedback.
All reactions
-
Guys@erf@uNetworkingAB, I am trying to serve html and it loads the page. But i am unable to load images that are stored in cloud. |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #997 on December 24, 2023 04:34.