Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pearofducks/podium-koa-podlet
Koa middleware for @podium/podlet
https://github.com/pearofducks/podium-koa-podlet
koa podium
Last synced: about 2 months ago
JSON representation
Koa middleware for @podium/podlet
- Host: GitHub
- URL: https://github.com/pearofducks/podium-koa-podlet
- Owner: pearofducks
- License: mit
- Created: 2019-04-25T06:38:10.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-04-26T06:19:29.000Z (almost 6 years ago)
- Last Synced: 2024-10-31T19:12:07.235Z (3 months ago)
- Topics: koa, podium
- Language: JavaScript
- Size: 4.88 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @podium/koa-podlet
Koa plugin for @podium/podlet.
Module for building [@podium/podlet] servers with [Koa]. For writing podlets,
please see the [Podium documentation].## Installation
```bash
$ npm install @podium/koa-podlet
```## Requirements
This module requires Koa v2.0.0 or newer.
## Simple usage
Build a simple podlet server:
```js
const { KoaPodlet } = require('@podium/koa-podlet');
const Koa = require('koa');
const _ = require('koa-route');
const Podlet = require('@podium/podlet');const app = Koa();
const podlet = new Podlet({
pathname: '/',
version: '2.0.0',
name: 'podletContent',
});app.use(KoaPodlet(podlet));
const contentRoute = _.get(podlet.content(), async ctx => {
if (ctx.state.podium.context.locale === 'nb-NO') {
ctx.podiumSend('Hei verden
');
return;
}
ctx.podiumSend('Hello world
');
});app.use(contentRoute);
const manifestRoute = _.get(podlet.manifest(), async ctx => {
ctx.body = podlet;
});app.use(manifestRoute);
const start = () => {
try {
const server = app.listen(7100)
console.log(`server listening on ${server.address().port}`)
} catch (err) {
console.error(err)
process.exit(1)
}
};
start();
```## Register plugin
The middleware is registered by passing an instance of the [@podium/podlet] class
to the middleware, and then registering that with Koa through `.use()`.```js
app.use(KoaPodlet(podlet));
```## Request params
On each request [@podium/podlet] will run a set of operations, such as
deserialization of the [@podium/context], on the request. When doing so
[@podium/podlet] will write parameters to `ctx.state.podium` which is
accessible inside a request handler.```js
const contentRoute = _.get(podlet.content(), async (ctx) => {
if (ctx.state.podium.context.locale === 'nb-NO') {
ctx.podiumSend('Hei verden
');
return;
}
ctx.podiumSend('Hello world
');
});
app.use(contentRoute);
```## ctx.podiumSend(fragment)
When in development mode this method will wrap the provided fragment in a
default HTML document before dispatching. When not in development mode, this
method will just dispatch the fragment.See [development mode] for further information.