@101-ways/core-express
v0.1.0
Published
Accelerator Express package
Downloads
3
Readme
@101-ways/core-express
Express server accelerator package. It uses the Service Registry pattern to create a single object with APIs.
Features
- starts server on port 8000 by default
- request / response logging with tracing metadata
- asynchronous context within request flow
Usage
import { load } from '@101-ways/core-express';
load([`${__dirname}/modules`]).then((sr) => {
sr.express.app.get('/hello', (req, res) => {
res.json({ result: 'hello world' });
});
});
Basic example for creating a child package:
import { load as loadCore, type Registry } from '@101-ways/express';
export type { Registry };
export async function load<T extends Registry>(paths: string[] = [], sr?: T) {
return loadCore([`${__dirname}/modules`, ...paths], sr);
}
Service Registry API
Everything in @101-ways/core and
- sr.config - configuration (see ./src/modules/config.ts)
- sr.express - Express module methods
- sr.express.app - Express app (see https://expressjs.com/en/4x/api.html#app)
- sr.express.returnError(req, res, err, code?) - helper function to log and return an error
- sr.express.server - HTTP server used by the app (see https://nodejs.org/api/http.html#class-httpserver)
Environment Variables
Everything in @101-ways/core and
- PORT='8000' - HTTP server port