@irwelljs/server-http-expressjs
v1.0.4
Published
## What is it? This is a wrapper around expressjs with the aim to invert control and create a simple, common interface. With this repository you can build your logic into a handler and forget that it is being served by http. This allows for a cleaner stru
Downloads
4
Readme
IrwellJS - Server HTTP expressjs
What is it?
This is a wrapper around expressjs with the aim to invert control and create a simple, common interface. With this repository you can build your logic into a handler and forget that it is being served by http. This allows for a cleaner structure, faster development, and the flexibility to serve content in another way without going through a large rewrite.
Basic concepts
You can define a handler that accepts validated data object as the first parameter. And then define a route which will hook the handler to the request via expressjs.
const route: RouteInterface = {
method: HttpMethod.POST,
path: '/my-route',
handler: myHandler,
validation: [
{
schema: myValidationSchema,
target: HttpVariableTarget.BODY,
}
],
};
The handler is a function that is called by express js. The validation schema is a Joi object that is run on the input data before it is passed into the handler. This means your focus can be placed on what data you need and what you need to do with it.
The wrapper will automatically handle errors and convert them to the correct error code. It will also take a returned object from the handler and convert into the correct response object. If you want to stop a request and return a bad response you only need to throw an error.