Static content alongside API
See original GitHub issueHi
I want to create an oak app with two parts:
- Backend (JSON API)
- Frontend (static files from a folder)
I thought I could create two Router
’s and set a different prefix
for each of the routers.
The first router (with prefix "/api"
) contains a bunch of routes/endpoints and is added to the Application with app.use(apiRouter.routes())
The second router (with prefix "/frontend"
) contains a use(...)
itself and pretty much follows this example: https://github.com/oakserver/oak/#static-content (but with frontendRouter.use(context ...
instead of app.use(context ...
)
I added the frontend router the same way as the API router (app.use(frontendRouter.routes())
)
Unsurprisingly, my approach didn’t work. Or more precise: The “static content delivery via a router”-part didn’t work.
Is there another approach to this (e.g., sub-applications or something like that)? Or is this use case just not covered at this phase of the project?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:10 (1 by maintainers)
@MartinMuzatko as for dynamic matching, I use this one successfully
where the
?
makes the parameter optional and*
matches “zero or more”, which’ll matchThough in your case maybe even just
router.get("/:slug*", async context => {...})
where/en/foo/bar/baz
wouldconsole.log(ctx.params)
=>{ slug: "en/foo/bar/baz" }
more at pillarjs/path-to-regexp
I can confirm that works @CanRau. I discovered that method recently on stackoverflow. Might be a good idea to officially document this method of serving static content under a path.