https://github.com/ajayos/node-server
A lightweight Express-based HTTP/HTTPS server wrapper with built-in middleware, lifecycle hooks, logging, and utility helpers.
https://github.com/ajayos/node-server
express express-js expressjs http http-server node
Last synced: 5 days ago
JSON representation
A lightweight Express-based HTTP/HTTPS server wrapper with built-in middleware, lifecycle hooks, logging, and utility helpers.
- Host: GitHub
- URL: https://github.com/ajayos/node-server
- Owner: Ajayos
- License: apache-2.0
- Created: 2023-07-30T10:20:04.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2026-01-26T17:07:29.000Z (2 months ago)
- Last Synced: 2026-01-27T04:47:41.235Z (2 months ago)
- Topics: express, express-js, expressjs, http, http-server, node
- Language: TypeScript
- Homepage:
- Size: 1.33 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Notice: NOTICE
Awesome Lists containing this project
README
# @ajayos/server
> A flexible, plugin-driven Express server wrapper with first-class TypeScript support, built-in middleware, and optional HTTPS.
[](https://www.npmjs.com/package/@ajayos/server)
[](./LICENSE)
---
## โจ Overview
`@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
---
## ๐ฆ Installation
```bash
npm install @ajayos/server
```
---
## ๐ Quick Start
```ts
import SERVER from "@ajayos/server";
const app = new SERVER(3000);
app.get("/", (_req, res) => {
res.send("Hello World");
});
app.start();
```
---
## ๐ง Flexible Constructors
All of the following are valid:
```ts
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"));
```
---
## โ๏ธ Server Configuration
```ts
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;
}
```
---
## ๐ HTTPS Support
```ts
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();
```
---
## ๐ Plugins System (3 Ways)
You can add plugins **in three different ways**.
---
### โ
1๏ธโฃ Via Config Flags (Simplest)
```ts
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.
---
### โ
2๏ธโฃ Via `config.plugins[]`
```ts
import { cors, bodyParser, static as serveStatic } from "@ajayos/server";
new SERVER({
port: 3000,
plugins: [cors({ origin: "*" }), bodyParser(), serveStatic("public")],
}).start();
```
---
### โ
3๏ธโฃ Via `usePlugin()` (Manual)
```ts
import { cors } from "@ajayos/server";
const app = new SERVER(3000);
app.usePlugin(cors({ origin: "*" }));
app.start();
```
---
## ๐ฆ Built-in Plugins List
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:
```ts
import { cors, helmet, bodyParser } from "@ajayos/server";
```
---
## ๐งฉ Plugin Usage Examples
### CORS
```ts
import { cors } from "@ajayos/server";
cors({ origin: "https://example.com" });
```
or via config:
```ts
new SERVER({
cors: { origin: "*" },
});
```
---
### Body Parser
```ts
bodyParser({ limit: "10mb" });
```
---
### Static Files
```ts
import { static as serveStatic } from "@ajayos/server";
serveStatic("public");
```
---
### Rate Limiting
```ts
rateLimit({
windowMs: 60_000,
max: 100,
});
```
---
### JSON Parse Error Handling
```ts
jsonError({ error: "Invalid JSON body" });
```
or with callback:
```ts
jsonError((err, req, res) => {
res.status(400).json({
message: err.message,
path: req.path,
});
});
```
> Only JSON parse errors are handled. Other errors pass through.
---
## ๐ง Express-Compatible Routing API
```ts
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);
```
---
## ๐ Events & Lifecycle APIs
### Server Events
```ts
app.on("error", (err) => {});
app.once("close", () => {});
app.off("error", handler);
```
### Express App Events
```ts
app.onApp("mount", () => {});
app.onceApp("mount", () => {});
app.offApp("mount", handler);
```
---
## ๐ Network Utilities
```ts
const interfaces = app.getActiveNetworkInterfaces();
```
Returns active IPv4 addresses (excluding localhost).
---
## ๐ Graceful Shutdown
```ts
app.close();
```
---
## ๐งช TypeScript Support
- Written in TypeScript
- Ships `.d.ts` files
- Fully typed plugin system
- Works with ESM and CommonJS
---
## ๐ License
Apache-2.0 License โ free to use, modify, and distribute.