express-service-bootstrap
license: Apache-2.0
license
license
Apache-2.0
Apache-2.0
This is a convenience package for starting a express based API server with
- General API Security - (helmet based)
- Health checks - K8S Health Probes
- Process exits listeners - Your API server should shutdown as gracefully as possible when it receives any shutdown signals from OS.
- Include your API documentation - Swagger UI express.
- Singleton DI container - possibly the best pattern to follow, yet completely flexible and ignorable.
- Creator pattern - don't use new keyword, this helps in writing better unit tests and mockable classes and features.
Getting Started
- Please find example code usage in examples folder
import { ApplicationBuilder, ApplicationStartupStatus, ApplicationTypes, Convenience } from "../../dist/src/index.js";
import * as OpenApiDefinition from "./api-def.json" with { type: "json" };
const applicationName = "Test Simple App";
const app = new ApplicationBuilder(applicationName, OpenApiDefinition);
const utilities = new Convenience();
async function AppStartUp(rootRouter, DIContainer, application) {
rootRouter
.get("/", (req, res) => {
res.send("Hello World");
})
.get("/error", (req, res) => {
throw new Error("This is an error to validate final 'ErrorResponseTransformer' error handling of library");
});
const apiDocs = utilities.swaggerAPIDocs(OpenApiDefinition);
application.overrideAppPort(8080)
.overrideHealthPort(8081)
.registerApplicationHandler(utilities.helmetMiddleware(), "*", 0, ApplicationTypes.Both)
.registerApplicationHandler(utilities.bodyParserURLEncodingMiddleware(), "*", 1, ApplicationTypes.Main)
.registerApplicationHandler(utilities.bodyParserJSONEncodingMiddleware({ limit: '50M' }), "*", 2, ApplicationTypes.Main)
.registerApplicationHandler(apiDocs.router, apiDocs.hostingPath, 3, ApplicationTypes.Main)
.overrideCatchAllErrorResponseTransformer((req, error) => ({
path: req.path,
status: 500,
body: { message: error.message }
}))
return {
status: ApplicationStartupStatus.UP,
data: { message: "Connected to database" }
};
}
app.overrideStartupHandler(AppStartUp)
.start()
.then(() => console.log(`${applicationName} started successfully.`))
.catch(console.error);
Built with
- Authors :heart: for Open Source.
Contributions
- New ideas/techniques are welcomed.
- Raise a Pull Request.
License
This project is contribution to public domain and completely free for use, view LICENSE.md file for details.