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 |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments 32 replies
-
No but it could possibly be done at some point, maybe. |
Beta Was this translation helpful? Give feedback.
-
Why not using Nginx for static files? |
Beta Was this translation helpful? Give feedback.
-
Why not cloudflare cdn? They also have websockets support among plenty of other things |
Beta Was this translation helpful? Give feedback.
-
@JemiloII You can look at here for ways to serve file/directory. Or use Cloudflare CDN as suggested by @nickchomey |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
Just started looking at this library and made a minimal file handler that worked for my case. I pushed it to a repo here import fs from 'node:fs/promises';
import mime from 'mime-types';
const sendFile = (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 } |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
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