Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/helloandre/tini
An extremely tiny js web framework
https://github.com/helloandre/tini
service
Last synced: 20 days ago
JSON representation
An extremely tiny js web framework
- Host: GitHub
- URL: https://github.com/helloandre/tini
- Owner: helloandre
- License: mit
- Created: 2019-06-04T06:32:26.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-05-01T06:54:47.000Z (over 1 year ago)
- Last Synced: 2024-10-27T17:30:36.242Z (2 months ago)
- Topics: service
- Language: JavaScript
- Size: 195 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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](https://github.com/pillarjs/path-to-regexp#readme)
**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