Created
July 9, 2023 16:06
-
-
Save longdog/12b78d8f8331591b10ff0a33d2376b35 to your computer and use it in GitHub Desktop.
Deno Oak Rest + SPA routing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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