A custom utility class for enhancing and managing behavior in Express-based HTTP servers. Includes CSRF setup, domain validation, IP extraction, and HTTP status code reference.
Alias for express.Request.
Represents a valid HTTP status code as a number.
(req: Request) => string[]A function that extracts a list of IPs from a request.
(req: Request) => booleanA function that validates if the domain of a request matches a rule.
Represents structured information parsed from an HTTP Origin header.
| Property | Type | Description | |
|---|---|---|---|
raw |
`string | null` | The raw Origin header value |
protocol |
string |
The protocol used (e.g., http, https) |
|
hostname |
string |
The hostname extracted from the origin | |
port |
string |
The port used (default or explicit) | |
full |
string |
The full reconstructed origin URL | |
error |
string |
Any parsing error encountered |
string = "hostname"Type of domain field to be validated (e.g., hostname).
string = "Forbidden domain."Message used when a domain is rejected.
{ [key: string]: IPExtractor }Registry of IP extractor functions.
string = "DEFAULT"Currently active IP extractor name.
{
refreshCookieName: string,
cookieName: string,
headerName: string,
errMessage: string,
enabled: boolean,
refreshInterval: number|null
}CSRF protection configuration object.
refreshCookieName: Name for the refresh token cookiecookieName: Name for the CSRF token cookieheaderName: Header where CSRF token is expectederrMessage: Error message on CSRF failureenabled: Whether CSRF is currently activerefreshInterval: Time interval (ms) for CSRF refresh
{ [key: string]: DomainValidator }A collection of validation functions per request property to validate incoming request domains.
readonly { [statusCode: number|string]: string }A lookup object for all standard and some common unofficial HTTP status codes with their descriptions. Includes:
- 📘 Informational:
100–103 - ✅ Successful:
200–226 - 🔁 Redirection:
300–308 - ❌ Client errors:
400–451 - 💥 Server errors:
500–511 - ☁️ Cloudflare-specific unofficial codes:
520–526
Example usage:
tinyExpress.#httpCodes[404]; // "Not Found"Creates and initializes a new TinyExpress instance.
-
✅ Injects domain validator middleware using
#domainTypeChecker. -
🔒 Automatically blocks invalid requests with
403 Forbidden. -
🌀 Registers loopback domains:
localhost,127.0.0.1, and::1. -
🛡️ Includes default domain validators for:
x-forwarded-hosthostnamehost
new TinyExpress(app);Parameters:
app(optional) — an existing Express app. Defaults toexpress().
Modifies a CSRF config option (except for internal controls like enabled).
Accepted keys:
"cookieName""headerName""errMessage"
tiny.setCsrfOption('cookieName', 'csrf_token');Sets how often the CSRF token should refresh.
tiny.setCsrfRefreshInterval(60000); // 1 minutePass null to disable refresh.
Returns a shallow clone of the current CSRF config.
const config = tiny.geCsrftOptions();Enables CSRF protection by generating and managing token cookies.
tiny.installCsrfToken(32, {
httpOnly: true,
sameSite: 'strict',
secure: true,
});Options:
bytes: token size in bytes (default24)httpOnly,sameSite,secure: cookie flags
Express middleware to validate incoming CSRF tokens.
app.use(tiny.verifyCsrfToken());Returns the TinyWebInstance.
const web = tiny.getWeb();Returns the current HTTP/HTTPS server instance.
const server = tiny.getServer();Returns the underlying Express application.
const app = tiny.getRoot();Initializes and links a TinyWebInstance (or raw server) to this wrapper.
tiny.init(); // uses default TinyWebInstanceAuto-registers:
- Default domains (
localhost, etc.) - Header-based domain validators
- IP extractors (
ip,ips,remoteAddress, etc.)
🔍 Parses the Origin header and returns a structured object.
const originInfo = appManager.getOrigin(req);Returns an object like:
{
raw: 'https://example.com',
protocol: 'https',
hostname: 'example.com',
port: '443',
full: 'https://example.com/'
}If the header is invalid, the result includes:
{ error: 'Invalid Origin header' }➕ Register a new IP extractor.
appManager.addIpExtractor('custom', (req) => extractIpList(req.headers['x-real-ip']));❗ Key
"DEFAULT"is reserved.
➖ Remove a registered extractor.
appManager.removeIpExtractor('custom');❗ You can't remove the one that's currently active.
📋 List all registered extractors.
const extractors = appManager.getIpExtractors();🔀 Set which extractor to use.
appManager.setActiveIpExtractor('custom');Use
"DEFAULT"to revert.
🎯 Returns the active extractor function.
📥 Extract IP using the active strategy.
const ipList = appManager.extractIp(req); // returns array of strings➕ Add a validator function for a domain check.
appManager.addDomainValidator('host', req => typeof req.headers.host === 'string' ? this.web.canDomain(req.headers.host) : false);❗
"ALL"is reserved.
➖ Remove a validator by key.
❗ Cannot remove the one currently active unless using
"ALL".
📋 Get all current domain validators.
🔧 Set which validator to use.
appManager.setDomainTypeChecker('host');
"ALL"applies all validators simultaneously.
📖 Get the default message for a status code.
const msg = appManager.getHttpStatusMessage(404); // 'Not Found'❓ Check if a status message exists.
➕ Add a custom HTTP status code.
appManager.addHttpCode(799, 'Custom Status');🚫 Cannot overwrite existing codes.
📤 Send a status error response.
appManager.sendHttpError(res, 404);Adds header HTTP/1.0 404 Not Found and ends the response.
🔧 Set up error handling in your Express app.
appManager.installErrors({
notFoundMsg: 'Oops! Nothing here.',
errNext: (status, err, req, res) => {
res.json({
status,
message: err.message,
stack: process.env.NODE_ENV === 'development' ? err.stack : null,
});
}
});Includes:
404handler usingHttpError- Global error formatter (with dev stacktrace!)
Authenticate incoming HTTP requests using Basic Auth.
Protect routes with a username/password combo. Optionally, use a custom validator or a fallback error middleware.
| Name | Type | Description |
|---|---|---|
req |
Request |
Express request object. |
res |
Response |
Express response object. |
next |
NextFunc |
Passes control to the next middleware if authentication succeeds. |
options |
Object |
Configuration object. |
options.login |
string |
Expected username. |
options.password |
string |
Expected password. |
options.nextError |
function | null |
Optional error handler middleware. |
options.validator |
(login, password) => boolean | Promise<boolean> |
Optional credential validation logic. |
- ✅ Calls
next()if credentials match. - ❌ Responds with
401 UnauthorizedandWWW-Authenticateif not. - 🎯 Falls back to
nextErrorif provided.
Send a file (as a Buffer) with proper HTTP headers. Ideal for downloads. 📥
Respond with a downloadable file, or inline content like logs, text files, or binary assets.
| Name | Type | Description |
|---|---|---|
res |
Response |
Express response object. |
options |
Object |
File response config. |
contentType |
string |
MIME type. Defaults to 'text/plain'. |
fileMaxAge |
number |
Cache-Control max-age in seconds. Defaults to 0. |
file |
Buffer |
File buffer to be sent. Required. |
lastModified |
Date | number | string | null |
Optional Last-Modified timestamp. |
fileName |
string | null |
Filename for download. Enables Content-Disposition. |
- 📦 Sets headers:
Content-Type,Content-Length,ETag,Last-Modified,Cache-Control. - 🧠 Automatically calculates
Content-MD5hash. - ⏳ If
fileMaxAge = 0, disables caching. - 📎 If
fileNameis provided, sets it asattachment.
Stream a file or readable stream with support for range requests. Great for audio/video! 🎥🎧
Serve large files with partial content support (Range headers), such as video playback.
| Name | Type | Description |
|---|---|---|
req |
Request |
Express request object (used to parse Range). |
res |
Response |
Express response object. |
options |
Object |
Streaming config. |
filePath |
string |
Full path to file on disk. Required if no stream. |
stream |
ReadableStream |
Alternative to filePath. Custom stream source. |
contentType |
string |
MIME type. Defaults to 'application/octet-stream'. |
fileMaxAge |
number |
Cache-Control max-age. Defaults to 0. |
lastModified |
Date | number | string | null |
Optional Last-Modified timestamp. |
fileName |
string | null |
Optional Content-Disposition. Can set inline. |
- 🎯 Detects
Rangeheader and sends partial content with206. - 💽 Full file streaming with
Content-Lengthif no range requested. - ⚙️ Automatically sets headers:
Accept-Ranges,Last-Modified,Cache-Control, andContent-Disposition.
A quick way to launch a test-only server environment in Express.js for static files and open CORS access.
- Serves static files from a folder you specify
- Enables unrestricted CORS (for all origins, headers, and methods)
- Only works in development mode (
NODE_ENV=development)
Do NOT use this in production!
This function:
- Disables all security headers
- Exposes everything to the public
- Should only be used for local testing or experiments
app.freeMode('./public');- The
sendFile()method is optimized for small/medium binary/text blobs that fit in memory. - Use
streamFile()for streaming video, audio, or large files efficiently. authRequest()supports both static credentials and async validation (e.g., DB or external checks).
- Node.js
fsandcrypto(for file handling and hashing) - Express.js