A minimal, highly performant middleware PSR-15 microframework built with as little complexity as possible, aimed primarily at those developers who want to understand all the vendors they use.
- node: 12
- @chubbyjs/psr-container: ^1.0.0
- @chubbyjs/psr-http-factory: ^1.1.0
- @chubbyjs/psr-http-message: ^1.2.1
- @chubbyjs/psr-http-server-handler: ^1.1.1
- @chubbyjs/psr-http-server-middleware: ^1.1.1
- @chubbyjs/psr-log: ^1.0.3
- @chubbyjs/chubbyjs-http-message: ^1.1.1
- @chubbyjs/chubbyjs-node-psr-http-message-bridge: ^1.2.3
- @chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge: ^1.1.1
Through NPM as @chubbyjs/chubbyjs-framework.
npm i @chubbyjs/[email protected] \
@chubbyjs/[email protected] \
@chubbyjs/[email protected]
import PathToRegexpRouteMatcher from '@chubbyjs/chubbyjs-framework-router-path-to-regexp/dist/PathToRegexpRouteMatcher';
import Application from '@chubbyjs/chubbyjs-framework/dist/Application';
import ErrorMiddleware from '@chubbyjs/chubbyjs-framework/dist/Middleware/ErrorMiddleware';
import RouteMatcherMiddleware from '@chubbyjs/chubbyjs-framework/dist/Middleware/RouteMatcherMiddleware';
import CallbackRequestHandler from '@chubbyjs/chubbyjs-framework/dist/RequestHandler/CallbackRequestHandler';
import Route from '@chubbyjs/chubbyjs-framework/dist/Router/Route';
import Routes from '@chubbyjs/chubbyjs-framework/dist/Router/Routes';
import ResponseFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/ResponseFactory';
import ResponseInterface from '@chubbyjs/psr-http-message/dist/ResponseInterface';
import ServerRequestInterface from '@chubbyjs/psr-http-message/dist/ServerRequestInterface';
const responseFactory = new ResponseFactory();
const app = new Application([
new ErrorMiddleware(responseFactory, true),
new RouteMatcherMiddleware(
new PathToRegexpRouteMatcher(new Routes([
Route.get('/hello/:name([a-z]+)', 'hello', new CallbackRequestHandler(
async (request: ServerRequestInterface): Promise<ResponseInterface> => {
const response = responseFactory.createResponse(200);
response.getBody().end(`Hello, ${request.getAttribute('name')}`);
return response;
},
)),
])),
responseFactory,
),
]);
npm i @chubbyjs/[email protected]
import Application from '@chubbyjs/chubbyjs-framework/dist/Application';
import ServerRequestFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/ServerRequestFactory';
import StreamFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/StreamFactory';
import UriFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/UriFactory';
import NodeResponseEmitter from '@chubbyjs/chubbyjs-node-psr-http-message-bridge/dist/NodeResponseEmitter';
import PsrRequestFactory from '@chubbyjs/chubbyjs-node-psr-http-message-bridge/dist/PsrRequestFactory';
import { createServer, IncomingMessage, ServerResponse } from 'http';
const app = new Application([...]);
const psrRequestFactory = new PsrRequestFactory(
new ServerRequestFactory(),
new UriFactory(),
new StreamFactory(),
);
const nodeResponseEmitter = new NodeResponseEmitter();
const server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
const serverRequest = psrRequestFactory.create(req);
const response = await app.handle(serverRequest);
nodeResponseEmitter.emit(response, res);
});
server.listen(8080, '0.0.0.0');
npm i @chubbyjs/[email protected]
import Application from '@chubbyjs/chubbyjs-framework/dist/Application';
import ServerRequestFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/ServerRequestFactory';
import StreamFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/StreamFactory';
import UriFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/UriFactory';
import PsrRequestFactory from '@chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge/dist/PsrRequestFactory';
import UwebsocketResponseEmitter from '@chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge/dist/UwebsocketResponseEmitter';
import { HttpRequest, HttpResponse } from 'uWebSockets.js';
const app = new Application([...]);
const psrRequestFactory = new PsrRequestFactory(
new ServerRequestFactory(),
new UriFactory(),
new StreamFactory(),
);
const uwebsocketResponseEmitter = new UwebsocketResponseEmitter();
require('uWebSockets.js')
.App()
.any('/*', async (res: HttpResponse, req: HttpRequest) => {
const serverRequest = psrRequestFactory.create(req, res);
const response = await app.handle(serverRequest);
uwebsocketResponseEmitter.emit(response, res);
})
.listen('0.0.0.0', 8080, (listenSocket: unknown) => {
if (listenSocket) {
console.log('Listening to port 0.0.0.0:8080');
}
});
Dominik Zogg 2021