A very simple http framework designed for Deno

A very simple http framework designed for Deno

This is a simple http framework like koa designed for Deno and TypeScript.This framework is a lazy one, not conclude too many features, but maybe useful in some situations.

Simple Koa

This is a simple http framework like koa designed for Deno and TypeScript.

This framework is a lazy one, not conclude too many features, but maybe useful in some situations.

Everything is strong typed.

Create Server

import { KServer, KRouter } from 'https://cdn.jsdelivr.net/gh/swwind/simple-koa/mod.ts';

type State = { name: string };
// create server
const app = new KServer<State>();
// set default state
app.state({ name: 'lumine' });
// create route
const router = new KRouter<State>();

If you felt terrible to write <State> so many times, you can write:

// create server and set default state
const app = new KServer({ name: 'lumine' });
// create router
const router = app.createRouter();

Router

Notice that you must write .status(200) in order not to get 404 by default.

router.use('/hello', async (ctx, nxt) => {
  ctx.res.status(200).text('hello world');
});
router.use('/world', async (ctx, nxt) => {
  ctx.res.status(200).json({ text: 'hello world' });
});
router.use('/index.html', async (ctx, nxt) => {
  ctx.res.status(200).html('<h1>hello world</h1>');
});
router.use('/jump', async (ctx, nxt) => {
  ctx.res.redirect('https://blog.swwind.me');
});

router.use<{ postid: string }>('/post/:postid', async (ctx, nxt) => {
  ctx.res.status(200).json(ctx.req.param); // {"postid":"..."}
});

const subrouter = app.createRouter();
subrouter.use('/you', async (ctx, nxt) => {
  ctx.res.status(200).text('I love you too');
});
subrouter.use('/emilia', async (ctx, nxt) => {
  ctx.res.status(200).text('I love emilia too');
});
router.use('/i/love', subrouter.routes());
// "/i/love/you"    => "I love you too"
// "/i/love/emilia" => "I love emilia too"

app.use(router.routes());

Listen

app.listen(4000);
app.listen(0); // random port

Cookie

router.use('/cookie', async (ctx, nxt) => {
  ctx.res.status(200).json(ctx.cookie.cookie);
});
router.use('/setcookie', async (ctx, nxt) => {
  ctx.cookie.set({ name: 'gawr', value: 'gura' });
  ctx.res.redirect('/cookie');
});
router.use('/deletecookie', async (ctx, nxt) => {
  ctx.cookie.remove('gawr');
  ctx.res.redirect('/cookie');
});

Download Details:

Author: swwind

Source Code: https://github.com/swwind/simple-koa

deno node nodejs javascript typescript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Enhanced scripting support for JavaScript/TypeScript with Deno

deno-script 🦕 Enhanced scripting support for JavaScript/TypeScript with Deno 🦕 on *nix-based systems.

Declarative schema validator, supports Node.js, TypeScript, and Deno

simple, easy-to-use, and declarative schema validator supports Node.js, TypeScript, and Deno

What is Deno? The Node.JS Killer? The Deno-minator to Node?

What is Deno? The Node.JS Killer? The Deno-minator to Node? Deno is the new hype and will it be better than node.js? Deno is a secure runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language that addresses Node’s shortcomings

What is Deno and is Node.js Dying? | Deno vs Node

What is Deno? Deno vs Node. Is Node.js going to die? Deno is a runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language. It was created by Ryan Dahl, original creator of Node.js, and is focused on productivity. It was announced by Dahl in 2018 during his talk "10 Things I Regret About Node.js"

Deno - How to Compile & Run Javascript Code in Deno Runtime

Learn How to Compile & Run Javascript Code in Deno Runtime.