Giter Club home page Giter Club logo

tini's Introduction

Tini

A tiny web framework

Installation

  • npm i @helloandre/tini

API

tini(fn)

a function that expects a function as it's only parameter, and is passed an object containing the routing api.

import tini from '@helloandre/tini'

tini(router => {
  router.get('/:key', req => req.params.key);
})

Router

Convenience methods

  • get(route: string, ...callbacks: Function)
  • post(route: string, ...callbacks: Function)
  • put(route: string, ...callbacks: Function)
  • del(route: string, ...callbacks: Function)
  • route(method: string, route: string, ...callbacks: Function) - a generic catch all for any other methods you may need to support

Nested Routers

  • with(router: Router)

Examples

For more in depth route path documentation, see path-to-regexp

Return String

tini(router => {
  router.get('/someroute', req => {
    return 'Hello, World!';
  });
});

Route Parameters + Query String

// url: /myKey?p=1
tini(router => {
  router.get('/:key', req => {
    // outputs "myKey, 1"
    return `${req.params.key}, ${req.query.p}`;
  });
});

Return JSON

tini(router => {
  router.get('/someroute', req => {
    return { hello: 'world' };
  });
});

Return A Promise

tini(router => {
  router.get('/someroute', req => {
    return Promise.resolve('hello, world');
  });
});

Return A Response

tini(router => {
  router.get('/someroute', req => {
    return new Response("Not Found", { status: 404 });
  });
});

Middleware

tini(router => {
  router.get('/someroute',
    req => {
      req.intermediateValue = 'somevalue';

      if (req.query.secret !== 'mysecret') {
        return new Response('Unauthorized', { status: 401 });
      }
    },
    req => {
      return req.intermediateValue;
    }
  );
});

Nested Routers

import tini, { Router } from '@helloandre/tini'

tini(router => {
  const api = new Router(`/api/v1`);
  api.get('/:name', (req) => ({ params: req.params, query: req.query }));
  router.with(api);

  router.get('(.*)', () => new Response("Not Found", { status: 404 }));
});

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.