0.0.4 • Published 6 years ago

@silkjs/silk v0.0.4

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

silk

guide

install

yarn add @silkjs/silk

example

http

import { Silk, Router } from '@silkjs/silk';

const app = new Silk();
const router = new Router();

// logger

app.use(async (ctx, next) => {
  await next();
  const rt = ctx.response.get('x-response-time');
  console.log(`[${ctx.method}] [${ctx.url}] - [${rt}]`);
});

// x-response-time

app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  ctx.set('x-response-time', `${ms}ms`);
});

// router

router.get('/', async ctx => {
  ctx.body = 'get';
});

router.get('/account/:id', async ctx => {
  ctx.body = {
    url: ctx.url,
    method: ctx.method,
    param: ctx.param,
    query: ctx.query,
    body: ctx.body
  };
});

router.post('/', async ctx => {
  ctx.body = 'post';
});

router.put('/', async ctx => {
  ctx.body = 'put';
});

router.patch('/', async ctx => {
  ctx.body = 'patch';
});

router.del('/', async ctx => {
  ctx.body = 'del';
});

app.use(router.routes());

// server
app.listen(80);

http2

import { readFileSync } from 'fs';
import { join } from 'path';
import { Silk, Router } from '@silkjs/silk';

const app = new Silk({
  key: readFileSync(join(__dirname, '../config/localhost-privkey.pem')),
  cert: readFileSync(join(__dirname, '../config/localhost-cert.pem'))
});

const router = new Router();

router.get('/account/:id', async ctx => {
  ctx.body = {
    url: ctx.url,
    method: ctx.method,
    param: ctx.param,
    query: ctx.query,
    body: ctx.body
  };
});

// server
app.listen(443);
0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago