HTTP Framework Compatibility
Podium is HTTP framework agnostic with first class support for Express.
In practise core Podium works with the standard http.Server module in Node.js, but the core modules also come with Express compatible middleware methods for ease of use.
Since Podium is built for the http.Server module in Node.js, it's pretty straight forward to get Podium to work with most HTTP frameworks.
The Podium team support Fastify via plugins.
- To write a podlet server with Fastify; please see @podium/fastify-podlet
- To write a layout server with Fastify; please see @podium/fastify-layout
Example of setting up a podlet server in all HTTP frameworks supported by the Podium team:
- Express
- Fastify
- HTTP
import express from 'express';
import Podlet, { html } from '@podium/podlet';
const app = express();
const podlet = new Podlet({
name: 'myPodlet',
version: '1.0.0',
pathname: '/',
development: true,
});
app.use(podlet.middleware());
app.get(podlet.content(), (req, res) => {
if (res.locals.podium.context.locale === 'nb-NO') {
return res.status(200).podiumSend(html`<h2>Hei verden</h2>`);
}
res.status(200).podiumSend(html`<h2>Hello world</h2>`);
});
app.get(podlet.manifest(), (req, res) => {
res.status(200).json(podlet);
});
app.listen(7100);
import fastifyPodlet from '@podium/fastify-podlet';
import fastify from 'fastify';
import Podlet, { html } from '@podium/podlet';
const app = fastify();
const podlet = new Podlet({
name: 'myPodlet',
version: '1.0.0',
pathname: '/',
development: true,
});
app.register(fastifyPodlet, podlet);
app.get(podlet.content(), async (request, reply) => {
if (reply.app.podium.context.locale === 'nb-NO') {
reply.podiumSend(html`<h2>Hei verden</h2>`);
return;
}
reply.podiumSend(html`<h2>Hello world</h2>`);
});
app.get(podlet.manifest(), async (request, reply) => {
reply.send(podlet);
});
const start = async () => {
try {
await app.listen(7100);
app.log.info(`server listening on ${app.server.address().port}`);
} catch (err) {
app.log.error(err);
process.exit(1);
}
};
start();
import { HttpIncoming } from '@podium/utils';
import Podlet from '@podium/podlet';
import http from 'http';
const podlet = new Podlet({
name: 'myPodlet',
version: '1.0.0',
pathname: '/',
development: true,
});
const server = http.createServer(async (req, res) => {
let incoming = new HttpIncoming(req, res);
incoming = await podlet.process(incoming);
if (incoming.url.pathname === podlet.manifest()) {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.setHeader('podlet-version', podlet.version);
res.end(JSON.stringify(podlet));
return;
}
if (incoming.url.pathname === podlet.content()) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.setHeader('podlet-version', podlet.version);
if (incoming.context.locale === 'nb-NO') {
res.end(podlet.render(incoming, html`<h2>Hei verden</h2>`));
return;
}
res.end(podlet.render(incoming, html`<h2>Hello world</h2>`));
return;
}
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('Not found');
});
server.listen(7100);