@thermopylae/core.adapter.express
v1.0.1
Published
Express adapter for Thermopylae framework.
Downloads
2
Maintainers
Readme
Express adapter for Thermopylae framework.
Install
npm install @thermopylae/core.adapter.express
Description
:information_source: TL;DR; Thermopylae framework allows you to use it's core.* modules with any of the existing web frameworks, such as express, fastify etc. All you need to do is implementing HttpRequest and HttpResponse interfaces from @thermopylae/core.declarations.
This package contains implementations for HttpRequest and HttpResponse from @thermopylae/core.declarations. These implementations are wrappers over Express request and response objects.
Usage
Bellow is a simple example of how this package can be used:
import { ExpressRequestAdapter, ExpressResponseAdapter, LOCATION_SYM } from '@thermopylae/core.adapter.express';
import { HttpStatusCode } from '@thermopylae/core.declarations';
import type { Request, Response } from 'express';
function handler(req: Request, res: Response) {
const request = new ExpressRequestAdapter(req);
const response = new ExpressResponseAdapter(res);
// location needs to be computed by yourself and attached to Express request object,
// before passing it to Thermopylae functions
request.raw[LOCATION_SYM] = {
countryCode: 'MD',
regionCode: 'K',
city: 'Chisinau',
latitude: null,
longitude: null,
timezone: null
};
console.log('New request received from ip ', request.ip, ' and device ', JSON.stringify(request.device));
response.status(HttpStatusCode.Ok).send('Hello World!');
}
API Reference
API documentation is available here.
It can also be generated by issuing the following commands:
git clone [email protected]:marinrusu1997/thermopylae.git
cd thermopylae
yarn install
yarn workspace @thermopylae/core.adapter.express run doc
Author
👤 Rusu Marin
- GitHub: @marinrusu1997
- Email: [email protected]
- LinkedIn: @marinrusu1997
📝 License
Copyright © 2021 Rusu Marin. This project is MIT licensed.