Skip to content

Instantly share code, notes, and snippets.

@longdog
Created July 9, 2023 16:06
Show Gist options
  • Save longdog/12b78d8f8331591b10ff0a33d2376b35 to your computer and use it in GitHub Desktop.
Save longdog/12b78d8f8331591b10ff0a33d2376b35 to your computer and use it in GitHub Desktop.
Deno Oak Rest + SPA routing
const app = new oak.Application();
// API routes
const apiRouter = new oak.Router({ prefix: "/api" });
apiRouter.get("/users", async (context) => {
context.response.body = await service.getUsers();
});
app.use(apiRouter.routes());
app.use(apiRouter.allowedMethods());
app.use(async (context) => {
// for front routing
await oak.send(context, "index.html", {
root: `${Deno.cwd()}/static`,
});
});
app.use(async (context) => {
// serve static files (css, js, ...)
await oak.send(context, context.request.url.pathname, {
root: `${Deno.cwd()}/static`,
});
});
app.listen({ port: 8000 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment