Add deno-oak starter (#214)

This commit is contained in:
Nebula
2022-01-28 11:25:38 -05:00
committed by GitHub
parent e9146d3021
commit ea062a2ab2
6 changed files with 78 additions and 0 deletions

5
examples/deno-oak/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
.env
.vscode/
coverage/
cov/
lcov/

View File

@@ -0,0 +1,15 @@
FROM denoland/deno:1.15.3
WORKDIR /app
USER deno
# Cache deps first
ADD deps.ts .
RUN deno cache deps.ts
# These steps will be re-run upon each file change in your working directory:
ADD . .
RUN deno cache src/index.ts
CMD deno run --allow-net --allow-env src/index.ts

View File

@@ -0,0 +1,26 @@
---
title: Deno with Oak
description: An example webserver using Oak
tags:
- typescript
---
# Deno Oak Example
This example is a small [Deno](https://deno.land/) webserver using [Oak](https://github.com/oakserver/oak) for routing.
[![Deploy on Railway](https://railway.app/button.svg)](https%3A%2F%2Frailway.app%2Fnew%3Ftemplate%3Dhttps%3A%2F%2Fgithub.com%2Frailwayapp%2Fexamples%2Ftree%2Fmaster%2Fexamples%2Fdeno-oak)
## ✨ Features
- Deno
- TypeScript
- Oak Routing
## 💁‍♀️ How to use
- [Install Deno](https://deno.land/)
- Run server `deno run --allow-net --allow-env src/index.ts`
## 📝 Notes
This is an experimental example and should not be used in production.

View File

@@ -0,0 +1 @@
export * from "https://deno.land/x/oak@v9.0.1/mod.ts";

View File

@@ -0,0 +1 @@
export {};

View File

@@ -0,0 +1,30 @@
import { Application, Router } from "../deps.ts";
const app = new Application();
const port = parseInt(Deno.env.get("PORT") ?? "8000");
// Router to serve specific functions on routes
const router = new Router();
// Route "ping" that responds with "Pong!"
router.get("/ping", ctx => {
ctx.response.body = "Pong!";
});
// Example of dynamic routes
router.get("/hello/:name", ctx => {
ctx.response.body = `Hello ${ctx.params.name}!`;
});
// Apply routes
app.use(router.routes());
app.use(router.allowedMethods());
// Basic example with a global route
// Applied last, so the router will take precedence
app.use(ctx => {
ctx.response.body = "Welcome to Railway!";
});
console.log(`Listening on port ${port}...`);
await app.listen({ port: port });