https://github.com/mhio/node-koa-web
Koa Web Sites
https://github.com/mhio/node-koa-web
Last synced: 3 months ago
JSON representation
Koa Web Sites
- Host: GitHub
- URL: https://github.com/mhio/node-koa-web
- Owner: mhio
- License: mit
- Created: 2020-11-10T00:06:12.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2020-11-10T04:11:59.000Z (over 4 years ago)
- Last Synced: 2025-01-30T12:28:35.365Z (5 months ago)
- Language: JavaScript
- Size: 124 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
@mhio/koa-web | [git](https://github.com/mhio/node-koa-web) | [npm](https://www.npmjs.com/package/@mhio/koa-web)
--------------------A Koa Web module to do all the request heavy lifting, so you just write logic/templates
Errors, logging, and templates are handled, just `return` the data or `throw` the errors
Based on [`@mhio/koa-web-handle`](https://github.com/mhio/node-koa-web-handle) under the hood.
## Install
```
yarn add @mhio/koa-web
npm install @mhio/koa-web
```## Usage
[API docs](doc/API.md)
```
const {KoaWeb, KoaWebHandler} = require('@mhio/koa-web')class MyHandler extends KoaWebHandler {
static templateWhateverThePath(ctx) {
return 'goes in `whatever` from /the/path'
}
static responseOveralls(ctx) {
return 'some html on /overalls'
}
}const web = new KoaWeb({ routes: MyHandler.routeConfig() })
web.listen().then(srv => console.log(srv))
```Or custom config can be injected as an array of `routes`
```
const {KoaWeb} = require('@mhio/koa-web')class MyHandler {
static get error_message(){
return 'Failure'
}static async ok(ctx){
return {
ok: true,
request_id, ctx.state.request_id,
}
}static async other(){
return 'other'
}static async error(){
throw new Error(this.error_message)
}}
// Route config for the API
const routes = [
// Simple function
[ 'get', '/ok', MyHandler.ok ],// Function bound to parent
[ 'get', '/ok2', MyHandler, 'ok' ],// Object setup
{ method: 'post', path: '/other', fn: MyHandler.other },// binds `MyHandler` for you
{ method: 'get', path: '/error', handler_object: MyHandler, handler_function: 'error' },
]
const web = new KoaWeb({ routes })
web.listen().then(srv => console.log(srv))
```## Related
[npm](https://www.npmjs.com/package/@mhio/koa-web)
[@mhio/koa-api](https://www.npmjs.com/package/@mhio/koa-api) /
[@mhio/koa-api-handle](https://www.npmjs.com/package/@mhio/koa-api-handle)[@mhio/koa-web](https://www.npmjs.com/package/@mhio/koa-web) /
[@mhio/koa-web-handle](https://www.npmjs.com/package/@mhio/koa-web-handle)