Objective HTTP Server

2.1.6 · active · verified Wed Apr 22

objective-http is a Node.js library that provides Object-Oriented Programming (OOP) proxy classes for constructing HTTP servers. It wraps Node.js's native `http` module, abstracting away low-level stream interactions into a more structured, class-based API. The library emphasizes a clear separation of concerns, allowing developers to define server logic through `Endpoint` classes (for route and request handling) and `Handler` classes (for error handling and request/response stream processing). Currently stable at version 2.1.6, the package appears to have a fairly active release cadence with frequent patch updates. It differentiates itself by offering a highly modular and extensible architecture for building HTTP services, including an `autoconfig` feature for simpler server setups, contrasting with more opinionated frameworks or purely functional approaches.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up a basic HTTP server using `objective-http`'s `autoconfig` utility, defining a simple GET endpoint, and starting the server.

const { server: autoconfigServer } = require('objective-http').server.autoconfig;
const { env } = require('node:process');

class MyEndpoint {
    route = {
        method: 'GET',
        path: '/hello'
    }

    async handle(request) {
        console.log(`Received request for: ${request.url}`);
        try {
            // Simulate some async processing
            await new Promise(resolve => setTimeout(resolve, 50));
            return {
                status: 200,
                body: 'Hello, Objective HTTP!'
            };
        
        } catch (e) {
            console.error('Error handling request:', e);
            return {
                status: 500,
                body: 'Internal server error'
            };
        }
    }
}

// Start the server using autoconfig
autoconfigServer({
    env: env,
    endpoints: [
        new MyEndpoint()
    ],
    options: { port: 3000 } // You can pass http.createServer options here
})
.then(() => console.log('Objective HTTP server listening on port 3000'))
.catch(err => console.error('Failed to start server:', err));

// Optional: Keep the process alive or graceful shutdown
// process.on('SIGINT', () => { console.log('Shutting down...'); process.exit(0); });

view raw JSON →