Request
Introduction
Every HTTP handler receives an Express Request object. JCC Express MVC extends it as AppRequest (jcc-express-mvc/lib/Interface): the same instance is passed as req in route closures and in { req, res, next } / httpContext patterns. After the framework’s request middleware runs, Laravel-style helpers are bound onto req (via HttpRequest in jcc-express-mvc/lib/Request/request.ts), so you call req.input(), req.header(), and so on.
You can also use the global request() helper, which resolves the current AppRequest from the container (only valid during an HTTP request, after the per-request binding middleware runs).
Types: import Request from jcc-express-mvc as the AppRequest alias when you need to annotate parameters (for example FormRequest subclasses). For res, response(), Inertia, flash, and redirectBack, see Response.md in this folder.
When helpers are available
Helpers are attached after the framework runs new HttpRequest(req, res) inside the global middleware stack (see Request lifecycle). Anything that runs before that middleware only sees a plain Express request.
Input from query, body, and merged data
These methods merge query and body for reads (input); file fields are handled separately via file / hasFile / store.
req.input()— All merged input, orreq.input(key, default?)for one key.req.has(key)— Whether the key exists in merged input.req.filled(key)— Present and non-empty string.req.only(...keys)— Subset of keys.req.except(...keys)— Copy of input without those keys (mutates the merged object for the copy path in the implementation—avoid relying on retaining the full original reference).req.merge(data)— Shallow assign ontoreq.body.req.replace(data)— Replacereq.bodyentirely.
Route parameters remain on req.params (see Routing). input() does not include params; use req.params.id or route model binding in controllers.
Headers, content negotiation, and API-ish detection
req.header(name)— Case-insensitive header lookup.req.ajax()—X-Requested-With: XMLHttpRequest.req.isJson()— JSONContent-Type.req.wantsJson(),req.acceptsJson()—Accepthints.req.expectsJson()— Combines API path prefix (/api), AJAX, and JSON signals.req.isInertia()— Presence ofX-Inertia.req.isApi()—truewhen the path starts with/api.
Use these to branch between JSON errors and HTML redirects, or to tailor Inertia responses.
Auth, session, and flash
req.user— Set by auth middleware when a user is authenticated (shape depends on your auth setup).req.session— Express session (requires session middleware).req.flash(...)— Flash API fromconnect-flash(same session stack).req.jccSession— Framework session bridge when available.- Global
session()returnsreq.jccSessionwhen you use the helper style.
Details: put / get / save / regenerate, flash alignment with res.with, and configuration notes are in Session.md.
Validation
req.validate(rules, customMessages?)— Validates against the current request; wired inHttpRequestto the sharedValidator(see framework validation docs /Validatormodule).req.validated()— Retrieves validated data after validation has run (when your pipeline stores it).
The global validate(...) helper resolves the current request and calls validate on it.
Files and cookies
req.hasFile(key)— Uploaded file field present (express-fileuploadstylereq.files).req.file(name)— Returns the request for chaining; thenreq.store(directory)on the samerequsessaveImageinternally (seeHttpRequestimplementation).req.cookie(key)— Parsed cookies (requirescookie-parser).
FormRequest subclasses can use protected file, store, cloudinaryFileUpload, etc., as defined on the base class.
Other helpers
req.bearerToken()— ParsesAuthorization: Bearer ….req.userAgent()—User-Agentheader.req.isMethod("post")— Case-insensitive HTTP method check.req.fullUrl()— Protocol, host, andoriginalUrl.req.csrfToken()— Available whencsrf()middleware ran; same value asres.locals._token(see CSRF-protection).
Express-native fields such as req.path, req.query, req.body, req.params, req.ip, and req.get("host") remain available.
FormRequest (action injection)
For validated or authorized form flows, extend FormRequest (jcc-express-mvc/Core/FormRequest) and type-hint the subclass on a controller method decorated with @Method(). The container constructs it with the current AppRequest, runs await rules() (your async or sync rules hook), then calls the action.
Typical pattern:
- Implement
protected async rules()(or sync) and callthis.validate({ field: ["required"], … })with rule arrays. - Optionally override
protected authorize()— the base implementation returnsfalse. If your override returnstrue, the constructor throwsAuthorizationException(deny). Returnfalseto allow the request to continue.
Expose domain actions such as save() on your subclass (UserRequest, etc.) and call them from the controller after injection.
Summary
AppRequest= ExpressRequest+ framework helpers bound per request.- Access via
req,request(), orhttpContext(parameter defaults only for context objects—see Routing). - Prefer
req.input/only/validatefor Laravel-like ergonomics; keepreq.paramsfor route segments and binding. - Sending the response:
Response.md(AppResponse,res.inertia,res.with,redirectBack, globalsview/redirect/back/inertia). - Session and flash:
Session.md.
