Skip to content
Sinatra like light weight web app framework for deno.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example Reversed path resolving order Feb 20, 2019
testdata/static
.gitignore
.travis.yml
LICENSE.md
Makefile
README.md
constants.ts Updated module paths Mar 7, 2019
errors.ts Changed to return error response based on status code Dec 1, 2018
handler.ts
mime.ts Added static file hosting Jan 3, 2019
mod.ts
response.ts Updated module paths Mar 7, 2019
serve_test.ts
static_test.ts

README.md

dinatra

Build Status

Usage

example/index.ts

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

app(
  get('/hello', () => 'hello'),
  get('/error', () => [500, 'an error has occured']),
  get('/callName', ({ params }) => `Hi, ${params.name}!`),
  post('/callName', ({ params }) => `Hi, ${params.name}!`),
  get('/info', () => [
    200,
    contentType('json'),
    JSON.stringify({ app: 'dinatra', version: '0.0.1' }),
  ])
);
deno index.ts --allow-net --allow-read # or --allow-all / -A
# App runs on localhost:8080

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

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/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

  • Files in ./public directory will be served static.

Stop server

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

// Stop API after 5000ms.
setTimeout(() => {
  api.close();
}, 5000);

Flags

deno 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.handle(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 | 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 of deno.land/x/net/http
interface HTTPResponse {
  status?: number;
  headers?: Headers;
  body?: Uint8Array | Reader;
}

Status

Request Params

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

Development

Testing

  • make test

Author

syumai

License

MIT

You can’t perform that action at this time.