Sinatra like light weight web app framework for deno

Sinatra like light weight web app framework for deno

Sinatra like light weight web app framework for deno Sinatra like light weight web app framework for Deno. This app is using Deno Standard Modules.

dinatra

Usage

example/index.ts

import {
  app,
  get,
  post,
  redirect,
  contentType,
} from "https://denopkg.com/syumai/[email protected]/mod.ts";

app(
  get("/hello", () => "hello"),
  get("/hello/:id", ({ params }) => params.id),
  get(
    "/hello/:id/and/:name",
    ({ params }) => `:id is ${params.id}, :name is ${params.name}`,
  ),
  get("/error", () => [500, "an error has occured"]),
  get("/callName", ({ params }) => `Hi, ${params.name}!`),
  post("/callName", ({ params }) => `Hi, ${params.name}!`),
  get("/foo", () => redirect("/hello", 302)), // redirect from /foo to /hello
  get("/info", () => [
    200,
    contentType("json"),
    JSON.stringify({ app: "dinatra", version: "0.0.1" }),
  ]),
);
deno run --allow-net --allow-read index.ts # Or simply: deno run -A index.ts
# App runs on localhost:8080

curl http://localhost:8080/hello
# status: 200
# body: hello

curl http://localhost:8080/hello/1
# status: 200
# body: 1

curl http://localhost:8080/hello/1/and/John
# status: 200
# body: :id is 1, :name is John

curl http://localhost:8080/error
# status: 500
# body: an error has occured

curl http://localhost:8080/callName?name=John
# status: 200
# body: Hi, John!

curl -d 'name=Tom' http://localhost:8080/callName
# status: 200
# body: Hi, Tom!

curl http://localhost:8080/foo
# status: 302
# location: /hello

curl http://localhost:8080/info
# status: 200
# content-type: application/json
# body: {"app":"dinatra","version":"0.0.1"}

Async Handler

  • You can use async function as handler.

example/template/index.ts

const { cwd, open } = Deno;
import { app, get } from 'https://denopkg.com/syumai/dinatra/mod.ts';

const currentDir = cwd();
const htmlPath = `${currentDir}/index.html`;

app(get('/', async () => await open(htmlPath)));

Template

  • You can use dejs (ejs for deno) as dinatra's template engine.
import { renderFile } from 'https://deno.land/x/dejs/dejs.ts';

app(
  get('/', async () => await renderFile('index.ejs', { message: 'example' }))
);

Host static files

  • Requires --allow-read, e.g. deno run --allow-read --allow-net app.ts
  • Files in ./public directory will be served static.

Close server

import { app, get } from 'https://denopkg.com/syumai/dinatra/mod.ts';

const s = app(get('/', () => 'hello'));

setTimeout(() => {
  s.close(); // close server after 5s.
}, 5000);

Flags

deno run -A index.ts -p 8000 # or --port=8000
# App runs on localhost:8000

Initialization options

Customize static file hosting option

import { defaultPort } from 'https://denopkg.com/syumai/dinatra/constants.ts';
import { App, get } from 'https://denopkg.com/syumai/dinatra/mod.ts';

const app = new App(
  defaultPort, // portNumber (number)
  'dist', // public file directory's path (string)
  false // option to enable static file hosting (boolean)
);

app.register(get('/hello', () => 'hello'));

Response Types

// HeaderMap is a type of response headers.
type HeaderMap =
  | Headers
  | {
      [key: string]: any;
    };

// ResponseBody is a type of response body.
type ResponseBody = string | ReadCloser | Deno.Reader;

/*
 *  Types of Response
 */

// StatusHeadersBodyResponse is a response with status code, headers, body.
type StatusHeadersBodyResponse = [number, HeaderMap, ResponseBody];

// StatusBodyResponse is a response with status code, body.
type StatusBodyResponse = [number, ResponseBody];

// Response is a type of response.
export type Response =
  | StatusHeadersBodyResponse
  | StatusBodyResponse
  | number // HTTP status code only
  | ResponseBody; // Response body only

// Response interface
interface HTTPResponse {
  status?: number;
  headers?: Headers;
  body?: Uint8Array | Deno.ReadCloser | Deno.Reader;
}

Status

Request Params

  • URL query params (for GET)
  • route params (like: /users/:user_id/posts)
  • x-www-form-urlencoded
  • redirect
  • application/json
  • application/octet-stream

Development

Update module

  • Please use dem
dem update https://deno.land/[email protected]

Lint

  • make lint

Format

  • make fmt

Testing

  • make test

Author

syumai

Download Details:

Author: syumai

Source Code: https://github.com/syumai/dinatra

deno nodejs node javascript

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

AEM Querybuilder for JavaScript (Browser, Node, Deno)

Helix Querybuilder .AEM Querybuilder for JavaScript (Browser, Node, Deno)

NKeys for JavaScript - Node.js, Browsers, and Deno

A public-key signature system based on Ed25519 for the NATS ecosystem system for JavaScript. The nkeys.js library works in Deno, Node.js, and the browser!

Javascript module for easily generating mazes,compatible with both Deno and Node

A work-in-progress Javascript maze generator module, compatible with both Deno and Node.

Deno Tutorial for Beginners: Deno vs. Node: Better Than NodeJS?

Deno is released, it’s time to find out if it can prove a worthy competition for Node.js. What is Deno? Why Deno can be considered an alternative to Node? The main differences between Deno and Node. Write a simple Deno app.

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