Files are not uploaded using nested paths
AlexanderZhirov opened this issue · comments
How do I make files visible for nested paths http://localhost/profile/test
?
router.get("/profile", &getReq);
router.get("/profile/test", &getReq);
router.get("*", serveStaticFiles(buildPath(datapath, "public")));
router.get("*", serveStaticFiles(buildPath(datapath, "images")));
router.get("*", serveStaticFiles(buildPath(datapath, "js")));
It doesn't load like that either. I do it according to this example.
Both css and js are loaded on the root page.
If I switch to /profile
, I catch 404, if I switch to /profile/test
, the page loads except css and js.
string datapath = "./";
auto router = new URLRouter;
router.get("/", &getReq);
router.get("*", serveStaticFiles(buildPath(datapath, "public")));
router.get("*", serveStaticFiles(buildPath(datapath, "images")));
router.get("*", serveStaticFiles(buildPath(datapath, "js")));
auto profile = new URLRouter("/profile");
profile.get("/", &getReqProfile);
profile.get("/test", &getReqProfile);
profile.get("*", serveStaticFiles(buildPath(datapath, "public")));
profile.get("*", serveStaticFiles(buildPath(datapath, "images")));
profile.get("*", serveStaticFiles(buildPath(datapath, "js")));
router.any("*", profile);
// / -> OK
// /profile -> 404
// /profile/ -> OK without css, js
// /profile/test -> OK without css, js
Why doesn't it upload files to the address /profile/test
? And how is it more correct to redirect /profile
to /profile/
? Register the handler /profile
how is getReqProfile
on router
?
Page 404 also does not load styles if the path is similar to /abc/asd/asd
, but if the path is single-level, then the styles are loaded. Is there a way to universally upload files under a single path?
In the files, dt
specified the path to the files from the root /style.css
and now, with any path from the root, files are loaded into the page, instead of style.css
. My mistake.
From the point of view of functionality, will a redirect to a nested router be considered correct?
router.get("/profile", staticRedirect("profile/", HTTPStatus.movedPermanently));
The staticRedirect
is a correct approach when using the URLRouter
directly. One thing to also consider, which can simplify the route setup considerably, is the vibe.web.web
module (registerWebInterface
). This will do the redirect automatically and can also infer the routes automatically. It would look something like this:
void main()
{
auto router = new URLRouter;
router.registerWebInterface(new ProfileService);
}
@path("/profile")
class ProfileService {
// automatically matched as GET /profile and /profile/
void get(HTTPServerRequest req, HTTPServerResponse res) {
//
}
// matched as GET /profile/test and /profile/test/
void getTest(HTTPServerRequest req, HTTPServerResponse res) {
//
}
}
If you are working with forms, it will also support conversion and validation of form fields by simply declaring them as method parameters.
@s-ludwig It looks almost what I need. And if I need to put the /test
call into a separate class in order to take out the processing functionality from the ProfileService
, is this possible?
@path("/profile/test")
class TestService {
void get(HTTPServerRequest req, HTTPServerResponse res) {
//
}
void post(HTTPServerRequest req, HTTPServerResponse res) {
//
}
}
Yes, that should work, registerWebInterface
can be called as many times as needed with the same URLRouter
, with different path prefixes and different class instances.
@s-ludwig Everything worked out, thanks a lot for your help!