express-service-bootstrap
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 listners - 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 ignoreable.
- Creator pattern - dont use new keyword, this helps in writting better unit tests and mockable classes and features.
Getting Started
- Please find example code usage in examples folder
import { ApplicationBuilder, ApplicationStartupStatus } from "../../dist/src/index.js";
import * as OpenApiDefinition from "./api-def.json" with { type: "json" };
import bodyParser from "body-parser";
const applicationName = "Test Simple App";
const app = new ApplicationBuilder(applicationName, OpenApiDefinition);
async function AppStartUp(rootRouter, DIContainer) {
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");
});
return {
status: ApplicationStartupStatus.UP,
data: { message: "Connected to database" }
};
}
app.overrideStartupHandler(AppStartUp)
.overrideAppPort(8080)
.overrideHealthPort(8081)
.overrideBodyParserJsonConfiguration(bodyParser.json({ limit: '50M' }))
.overrideCatchAllErrorResponseTransformer((req, error) => ({
path: req.path,
status: 500,
body: { message: error.message }
}))
.start()
.then(() => console.log("Application started successfully."))
.catch(console.error);
main().catch(console.error);
Built with
- Authors :heart: for Open Source.
Contributions
- New ideas/techniques are welcomed.
- Raise a Pull Request.
License
This project is contrubution to public domain and completely free for use, view LICENSE.md file for details.