question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Static content alongside API

See original GitHub issue

Hi

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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
CanRaucommented, Oct 29, 2021

@MartinMuzatko as for dynamic matching, I use this one successfully

router.get("/:lang?/:slug*", async context => {...})

where the ? makes the parameter optional and * matches “zero or more”, which’ll match

/en
/en/slug
/en/slug/whatever
/en/slug/whatever/ever

Though in your case maybe even just router.get("/:slug*", async context => {...}) where /en/foo/bar/baz would console.log(ctx.params) => { slug: "en/foo/bar/baz" }

more at pillarjs/path-to-regexp

1reaction
MartinMuzatkocommented, Oct 29, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding dynamic content from an API to a static website at ...
You may not need client side JavaScript to add dynamic content from an API. In this article I will show the approach I...
Read more >
Serving static content via Web API - Stack Overflow
I'm currently using WebAPI running on OWIN/Katana (on a Windows Azure worker role instance). The client (homepage build with HTML, CSS & ...
Read more >
How to serve Static WebSite content through Dot Net core ...
I have a Dot Net Core Web API developed , deployed and running in Azure. It serves as API Gateway for now. I...
Read more >
Using Static Files (HTML, JavaScript) In Web API - C# Corner
In this article, we will see how to add static files or groups of static files to an existing ASP.Net Core web API...
Read more >
How to Build an API-Powered, Static Website - Cosmic JS
A rebuild process that makes it easy to rebuild our app after content edits. 3. The ability to create pages using Markdown. To...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found