- Sinatra like light weight web app framework for deno.
- This app is using Deno Standard Modules.
- All features of this app are currently experimental.
example/index.ts
import {
app,
get,
post,
contentType,
} from 'https://syumai.github.io/dinatra/dinatra.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
# 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"}- You can use async function as handler.
import { app, get } from '../../dinatra.ts';
import { cwd, open } from 'deno';
const currentDir = cwd();
const htmlPath = `${currentDir}/index.html`;
app(get('/', async () => await open(htmlPath)));- You can use dejs (ejs for deno) as dinatra's template engine.
import { renderFile } from 'https://syumai.github.io/dejs/dejs.ts';
app(
get('/', async () => await renderFile('index.ejs', { message: 'example' }))
);deno index.ts -p 8000 # or --port=8000
# App runs on localhost:8000// 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;
}- URL query params (for GET)
- route params (like:
/users/:user_id/posts) - x-www-form-urlencoded
- application/json