Complete HTTP server functionality including routing, middleware, request/response handling, and body parsing. AdonisJS Core re-exports the full @adonisjs/http-server package with additional integration features.
HTTP routing system for defining application routes with various HTTP methods.
/**
* HTTP Router for defining application routes
*/
class Router {
/**
* Register GET route
* @param pattern - Route pattern (e.g., "/users/:id")
* @param handler - Route handler function or controller reference
* @returns Route instance for further configuration
*/
get(pattern: string, handler: RouteHandlerNode): Route;
/**
* Register POST route
* @param pattern - Route pattern
* @param handler - Route handler function or controller reference
* @returns Route instance for further configuration
*/
post(pattern: string, handler: RouteHandlerNode): Route;
/**
* Register PUT route
* @param pattern - Route pattern
* @param handler - Route handler function or controller reference
* @returns Route instance for further configuration
*/
put(pattern: string, handler: RouteHandlerNode): Route;
/**
* Register DELETE route
* @param pattern - Route pattern
* @param handler - Route handler function or controller reference
* @returns Route instance for further configuration
*/
delete(pattern: string, handler: RouteHandlerNode): Route;
/**
* Register PATCH route
* @param pattern - Route pattern
* @param handler - Route handler function or controller reference
* @returns Route instance for further configuration
*/
patch(pattern: string, handler: RouteHandlerNode): Route;
/**
* Register route for any HTTP method
* @param methods - Array of HTTP methods
* @param pattern - Route pattern
* @param handler - Route handler function or controller reference
* @returns Route instance for further configuration
*/
route(methods: string[], pattern: string, handler: RouteHandlerNode): Route;
/**
* Create route group for applying common configuration
* @param callback - Function to define routes within the group
* @returns Route group instance
*/
group(callback: () => void): RouteGroup;
/**
* Define resource routes for RESTful operations
* @param name - Resource name
* @param controller - Controller class or string reference
* @returns Resource route instance
*/
resource(name: string, controller: string): ResourceRoute;
}Usage Examples:
import { Router } from "@adonisjs/core/http";
const router = Router;
// Basic routes
router.get("/", () => "Hello World");
router.post("/users", "UsersController.store");
router.get("/users/:id", "UsersController.show");
// Route groups
router.group(() => {
router.get("/profile", "UsersController.profile");
router.put("/profile", "UsersController.updateProfile");
}).prefix("/api/v1").middleware("auth");
// Resource routes
router.resource("posts", "PostsController");HTTP server management and lifecycle.
/**
* HTTP Server class for managing server lifecycle
*/
class Server {
/**
* Start the HTTP server
* @param callback - Optional callback when server is ready
*/
start(callback?: () => void): Promise<void>;
/**
* Close the HTTP server gracefully
*/
close(): Promise<void>;
/**
* Get server instance
* @returns Node.js HTTP server instance
*/
getNodeServer(): NodeServer;
/**
* Get server address
* @returns Server address information
*/
address(): AddressInfo | null;
}HTTP request object with extensive functionality.
/**
* HTTP Request class with body parsing and validation support
*/
class Request {
/** Request URL */
url(): string;
/** HTTP method */
method(): string;
/** Request headers */
headers(): Record<string, string>;
/** Query parameters */
qs(): Record<string, any>;
/** Route parameters */
params(): Record<string, any>;
/** Request body (parsed) */
body(): any;
/** Get all request data (query + body + params) */
all(): Record<string, any>;
/** Get specific input value */
input(key: string, defaultValue?: any): any;
/** Check if request has specific input */
has(key: string): boolean;
/** Get uploaded files */
file(key: string): MultipartFile | null;
/** Get all uploaded files */
allFiles(): Record<string, MultipartFile>;
/** Validate request data */
validate<T>(schema: ValidationSchema<T>): Promise<T>;
}HTTP response object for sending responses.
/**
* HTTP Response class for sending responses
*/
class Response {
/**
* Set response status code
* @param code - HTTP status code
*/
status(code: number): this;
/**
* Set response header
* @param key - Header name
* @param value - Header value
*/
header(key: string, value: string): this;
/**
* Send JSON response
* @param data - Data to send as JSON
*/
json(data: any): void;
/**
* Send plain text response
* @param text - Text to send
*/
send(text: string): void;
/**
* Redirect to URL
* @param url - URL to redirect to
* @param status - HTTP status code (default: 302)
*/
redirect(url: string, status?: number): void;
/**
* Download file
* @param filePath - Path to file
* @param fileName - Optional filename for download
*/
download(filePath: string, fileName?: string): void;
/**
* Stream file
* @param filePath - Path to file
*/
stream(filePath: string): void;
}Middleware system for request/response processing.
/**
* HTTP Context passed to middleware and route handlers
*/
interface HttpContext {
request: Request;
response: Response;
params: Record<string, any>;
route?: Route;
session?: Session;
auth?: AuthManager;
}
/**
* Middleware function type
*/
type MiddlewareFn = (
ctx: HttpContext,
next: NextFn
) => Promise<void> | void;
type NextFn = () => Promise<void>;Request validation utilities integrated with AdonisJS Core.
/**
* Request validator for validating HTTP requests
*/
class RequestValidator {
/**
* Validate request data against schema
* @param request - HTTP request instance
* @param schema - Validation schema
* @returns Validated data
*/
static validate<T>(request: Request, schema: ValidationSchema<T>): Promise<T>;
/**
* Validate query parameters
* @param request - HTTP request instance
* @param schema - Validation schema
* @returns Validated query data
*/
static validateQuery<T>(request: Request, schema: ValidationSchema<T>): Promise<T>;
/**
* Validate request body
* @param request - HTTP request instance
* @param schema - Validation schema
* @returns Validated body data
*/
static validateBody<T>(request: Request, schema: ValidationSchema<T>): Promise<T>;
}Usage Examples:
import { RequestValidator } from "@adonisjs/core/http";
import { schema, rules } from "@adonisjs/validator";
// In a controller method
export default class UsersController {
async store({ request, response }: HttpContext) {
const data = await RequestValidator.validate(request, {
name: schema.string([rules.minLength(2)]),
email: schema.string([rules.email()]),
age: schema.number([rules.range(18, 100)])
});
// Create user with validated data
const user = await User.create(data);
return response.json(user);
}
}Request body parsing with file upload support.
/**
* Multipart file interface for uploaded files
*/
interface MultipartFile {
/** Original filename */
clientName: string;
/** File size in bytes */
size: number;
/** MIME type */
type: string;
/** File extension */
extname: string;
/** Temporary file path */
tmpPath: string;
/** Move file to destination */
move(destination: string, options?: MoveOptions): Promise<void>;
/** Check if file is valid */
isValid: boolean;
/** File validation errors */
errors: Error[];
}
interface MoveOptions {
name?: string;
overwrite?: boolean;
}type RouteHandlerNode =
| ((ctx: HttpContext) => any)
| string
| [string, string];
interface Route {
as(name: string): this;
middleware(middleware: string | string[]): this;
where(key: string, matcher: string | RegExp): this;
prefix(prefix: string): this;
}
interface RouteGroup {
prefix(prefix: string): this;
middleware(middleware: string | string[]): this;
as(name: string): this;
where(matchers: Record<string, string | RegExp>): this;
}
interface ResourceRoute {
only(actions: string[]): this;
except(actions: string[]): this;
middleware(middleware: Record<string, string | string[]>): this;
as(name: string): this;
}
interface ValidationSchema<T> {
[K in keyof T]: ValidationRule;
}
interface ValidationRule {
// Specific validation rule implementation
}
interface AddressInfo {
port: number;
family: string;
address: string;
}
interface NodeServer {
listen(port?: number, hostname?: string, callback?: () => void): this;
close(callback?: (err?: Error) => void): this;
}