anyok
v1.1.1
Published
HTTP service that accepts any request and responds with 'ok' while logging requests with pretty formatting
Downloads
19
Maintainers
Readme
http-anyok
HTTP service that accepts any request and responds with customizable messages while logging requests with pretty formatting.
Features
- ✅ Accepts any HTTP method (GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD)
- ✅ Customizable response messages (text or JSON)
- ✅ Default "ok" response when no custom response is set
- ✅ Pretty formatted request logging to console
- ✅ Real-time statistics web interface with request monitoring
- ✅ Advanced filtering with regex support and exclude functionality
- ✅ Server-Sent Events for live request updates
- ✅ Configurable server port and timeout
- ✅ CORS enabled for cross-origin requests
- ✅ Graceful shutdown handling
- ✅ Clean, modular architecture
- ✅ Comprehensive test coverage
- ✅ TypeScript definitions included
Installation
Global Installation
npm install -g http-anyokLocal Installation
npm install http-anyokUsage
Command Line Interface
# Start server on default port (3000)
http-anyok
# Start server on specific port
http-anyok --port 8080
http-anyok -p 8080
# Start server with custom text response
http-anyok --response "Hello World"
http-anyok -r "Custom message"
# Start server with JSON response
http-anyok --responseJson '{"status":"success","message":"API is running"}'
# Combine options
http-anyok --port 8080 --response "Server running on port 8080"
http-anyok -p 3001 --responseJson '{"port":3001,"status":"online"}'
# Set request timeout (in seconds)
http-anyok --timeout 30
http-anyok -t 30
# Enable statistics server (default port 9989)
http-anyok --stats
# Enable statistics server with custom port
http-anyok --stats --stats-port 8081
# Combine with other options
http-anyok --port 3000 --stats --stats-port 9989 --response "API Server"
# Show help
http-anyok --helpProgrammatic Usage
import { ServerController, StatisticsServer } from 'http-anyok';
// Create and start server with default response
const server = new ServerController(3000);
server.start();
// Create server with custom text response
const serverWithText = new ServerController(3000, 0, null, 'Hello from API');
serverWithText.start();
// Create server with JSON response
const jsonResponse = '{"status":"success","message":"API ready"}';
const serverWithJson = new ServerController(3000, 0, jsonResponse, null);
serverWithJson.start();
// Create server with statistics monitoring
const statsServer = new StatisticsServer(9989);
statsServer.start();
const serverWithStats = new ServerController(3000, 0, null, null, statsServer);
serverWithStats.start();
// Graceful shutdown
process.on('SIGINT', () => {
server.shutdown();
statsServer?.stop();
});API
ServerController
Main server class that handles HTTP requests.
import { ServerController } from 'http-anyok';
// Constructor parameters:
// ServerController(port, timeout, responseJson, response)
const server = new ServerController(3000, 30, null, 'Custom response');
server.start();Parameters:
port(number, default: 3000) - Server porttimeout(number, default: 0) - Request timeout in secondsresponseJson(string, default: null) - JSON string to parse and send as JSON responseresponse(string, default: null) - Plain text response messagestatisticsServer(StatisticsServer, default: null) - Optional statistics server instance for request monitoring
StatisticsServer
Statistics server for monitoring HTTP requests with web interface.
import { StatisticsServer } from 'http-anyok';
// Constructor parameters:
// StatisticsServer(port)
const statsServer = new StatisticsServer(9989);
statsServer.start();Parameters:
port(number, default: 9989) - Statistics server port
RequestModel
Data model for HTTP requests used in logging.
import { RequestModel } from 'http-anyok';
// Automatically created from incoming requests
const requestModel = new RequestModel(req);
console.log(requestModel.toJSON());Logger
Pretty request logger with colored output.
import { Logger } from 'http-anyok';
const logger = new Logger();
logger.logRequest(requestModel);
logger.logServerStart(port, responseJson, response);Server Startup
The server displays a colorful startup message showing configuration:
✓ HTTP AnyOK Server Started
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Port: 3000
URL: http://localhost:3000
Status: Accepting all requests
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Stats: Enabled
Stats Port: 9989
Stats URL: http://localhost:9989
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Response: JSON {"status":"success","message":"API ready"}
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Press Ctrl+C to stop the serverRequest Logging
All requests are logged with pretty formatting including:
- Request timestamp
- HTTP method (color-coded)
- Request URL
- User Agent
- Remote IP address
- Content type and length
- Important headers (authorization, cookies, etc.)
Example log output:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Request #1
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Time: 2024-01-15T10:30:45.123Z
Method: GET
URL: /api/test
From: 127.0.0.1
Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
Content: application/json (150 bytes)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━Response Customization
Command Line Options
| Option | Short | Description | Example |
| ---------------- | ----- | ------------------------- | ---------------------------------- |
| --response | -r | Plain text response | --response "Hello World" |
| --responseJson | | JSON response (parsed) | --responseJson '{"status":"ok"}' |
| --port | -p | Server port | --port 8080 |
| --timeout | -t | Request timeout (seconds) | --timeout 30 |
| --stats | | Enable statistics server | --stats |
| --stats-port | | Statistics server port | --stats-port 9989 |
Response Priority
- JSON Response: If
--responseJsonis provided, it takes priority - Text Response: If
--responseis provided (and no JSON), uses text - Default: If neither is provided, responds with "ok"
Statistics Server
When enabled with --stats, the statistics server provides a real-time web interface to monitor HTTP requests.
Features
- Real-time Request Monitoring: See requests as they arrive via Server-Sent Events
- Request Details: View headers, body, response time, and metadata
- Advanced Filtering: Filter by path, HTTP method with regex and exclude support
- Collapsible Interface: Click to expand/collapse request details
- Live Statistics: Total requests, average response time, last request time
Usage
# Enable statistics server
http-anyok --stats
# Statistics will be available at http://localhost:9989Filter Examples
Basic Filtering:
- Path:
/api- Show only requests containing "/api" - Method:
GET- Show only GET requests
Regex Filtering:
- Enable "Regex" checkbox and use patterns like:
^/api/.*- All URLs starting with /api//users/\d+$- User URLs with numeric IDs\.(css|js|png)$- Static files/v[1-3]/.*- API versions 1, 2, or 3
Exclude Filtering:
- Enable "Exclude" checkbox to hide matching requests:
- Exclude path
/health- Hide health check requests - Exclude method
OPTIONS- Hide preflight requests
- Exclude path
Combined Filtering:
- Path:
/api+ Method:POST- Only API POST requests - Regex:
^/api/.*+ Exclude method:GET- API requests except GET - Exclude path:
/static+ Exclude method:OPTIONS- Hide static files and preflight
Development
Setup
git clone <repository-url>
cd http-anyok
npm installTesting
npm test
npm run test:watch
npm run test:coverageLinting
npm run lintDevelopment Mode
npm run devLicense
MIT
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
