A flexible, plugin-driven Express server wrapper with first-class TypeScript support, built-in middleware, and optional HTTPS.
@ajayos/server is a thin, powerful wrapper around Express that helps you:
- Start HTTP or HTTPS servers easily
- Enable common middleware using simple config flags
- Extend behavior using a plugin system
- Use flexible constructors (
port,config, callbacks) - Keep server bootstrap code clean and readable
Designed to be:
- β Simple by default
- π Plugin-driven when needed
- π§ TypeScript-first
- π Production-ready
npm install @ajayos/serverimport SERVER from "@ajayos/server";
const app = new SERVER(3000);
app.get("/", (_req, res) => {
res.send("Hello World");
});
app.start();All of the following are valid:
new SERVER(3000);
new SERVER(3000, () => console.log("started"));
new SERVER(3000, { cors: true });
new SERVER({ port: 3000 });
new SERVER({ port: 3000 }, () => console.log("started"));interface ServerConfig {
port?: number;
https?: {
key: string | Buffer;
cert: string | Buffer;
};
// Built-in middleware flags
cors?: boolean | object;
helmet?: boolean | object;
bodyParser?: boolean | object;
compression?: boolean | object;
morgan?: boolean | string;
rateLimit?: boolean | object;
timeout?: boolean | object;
static?: string;
// Plugins
plugins?: ServerPlugin[];
// Lifecycle hooks
onServerStart?: () => void;
onServerError?: (error: any) => void;
}import fs from "fs";
import SERVER from "@ajayos/server";
new SERVER({
port: 8443,
https: {
key: fs.readFileSync("./key.pem"),
cert: fs.readFileSync("./cert.pem"),
},
}).start();You can add plugins in three different ways.
new SERVER({
port: 3000,
cors: true,
helmet: true,
bodyParser: true,
compression: true,
morgan: "dev",
static: "public",
}).start();Built-in flags are internally converted into plugins automatically.
import { cors, bodyParser, static as serveStatic } from "@ajayos/server";
new SERVER({
port: 3000,
plugins: [cors({ origin: "*" }), bodyParser(), serveStatic("public")],
}).start();import { cors } from "@ajayos/server";
const app = new SERVER(3000);
app.usePlugin(cors({ origin: "*" }));
app.start();Available out of the box:
| Plugin | Description |
|---|---|
cors() |
Cross-Origin Resource Sharing |
helmet() |
Security headers |
bodyParser() |
JSON + URL-encoded body parsing |
compression() |
Gzip / Brotli compression |
morgan() |
HTTP request logging |
rateLimit() |
Request rate limiting |
timeout() |
Request timeout handling |
static() |
Static file serving |
jsonError() |
JSON parse error handling |
All plugins are available from:
import { cors, helmet, bodyParser } from "@ajayos/server";import { cors } from "@ajayos/server";
cors({ origin: "https://example.com" });or via config:
new SERVER({
cors: { origin: "*" },
});bodyParser({ limit: "10mb" });import { static as serveStatic } from "@ajayos/server";
serveStatic("public");rateLimit({
windowMs: 60_000,
max: 100,
});jsonError({ error: "Invalid JSON body" });or with callback:
jsonError((err, req, res) => {
res.status(400).json({
message: err.message,
path: req.path,
});
});Only JSON parse errors are handled. Other errors pass through.
app.use(middleware);
app.get("/users", handler);
app.post("/users", handler);
app.put("/users/:id", handler);
app.delete("/users/:id", handler);
app.patch("/users/:id", handler);
app.all("/health", handler);app.on("error", (err) => {});
app.once("close", () => {});
app.off("error", handler);app.onApp("mount", () => {});
app.onceApp("mount", () => {});
app.offApp("mount", handler);const interfaces = app.getActiveNetworkInterfaces();Returns active IPv4 addresses (excluding localhost).
app.close();- Written in TypeScript
- Ships
.d.tsfiles - Fully typed plugin system
- Works with ESM and CommonJS
Apache-2.0 License β free to use, modify, and distribute.