expressjs/expressMIT9a34acfReport / request removal

Built-in Middleware

Express exposes middleware for parsing common request bodies and serving files from a directory. Its public middleware surface includes JSON, URL-encoded, raw, text, and static-file helpers.

These helpers exist so applications can add standard request processing with app.use(...), while route handlers work with prepared request data or requested files. The application factory creates an app function that delegates incoming requests to app.handle.

Sources: test/exports.js:12-29, lib/express.js:36-56

Core concepts

Body-parsing middleware

Body-parsing middleware reads a request body and makes the resulting value available through req.body. The JSON and URL-encoded test helpers mount a parser before a POST handler that reads this property.

Sources: test/express.json.js:722-740, test/express.urlencoded.js:805-822

body-parser

The shown Express implementation imports the npm package body-parser as bodyParser. This is the only package identity explicitly established by the supplied source; the package implementing static-file middleware is not shown.

Sources: lib/express.js:15

Static middleware

Static middleware checks for a file matching the request path below a configured directory. The example maps GET /js/app.js to ./public/js/app.js under the mounted public directory.

Sources: examples/static-files/index.js:15-22

Request body shape

The parser determines the value placed in req.body: raw parsing produces a Buffer, while the JSON and URL-encoded examples pass req.body to a JSON response helper.

The built-in middleware surface can be compared as follows.

MiddlewareInput or purposeObserved resultPackage evidence
JSON helperJSON request bodiesHandler returns req.body through a JSON responseExpress imports body-parser
URL-encoded helperURL-encoded request bodiesHandler returns req.body through a JSON responseExpress imports body-parser
Raw helperBinary request bodiesreq.body is a Buffer for application/octet-streamPackage identity is not shown
Text helperText request bodiesExposed as a built-in functionPackage identity is not shown
Static helperFiles under a configured directoryMiddleware is mounted before a 404 handlerPackage identity is not shown

Sources: test/express.raw.js:65-83, test/express.json.js:722-740, test/express.urlencoded.js:805-822, test/exports.js:12-29, lib/express.js:15, test/express.static.js:804-815

How body parsing changes the request

A body parser is installed as middleware before the route that reads the body. In the JSON test helper, the JSON middleware is passed to app.use, then a POST handler sends req.body back through a JSON response helper. The URL-encoded helper follows the same structure with the URL-encoded middleware.

The important hand-off is therefore from parser middleware to the route handler: the handler reads req.body rather than reading the request stream directly in the shown examples. The supplied excerpts do not establish the exact object shape produced for URL-encoded input, so this page makes no stronger claim.

Raw parsing differs visibly. For a request with content type application/octet-stream, the test verifies that req.body is a Buffer and converts it to hexadecimal text. Raw parsing therefore preserves the tested body as binary data instead of producing the parsed value used by the JSON and URL-encoded examples.

The JSON and URL-encoded helpers also install an error handler before their route. That handler reads parser error properties, sets the response status, and sends a textual representation of the error. This placement gives parser failures a handler in the middleware chain before the normal POST handler is used.

The request-side flow is summarized here.

Body parsing flow — How does a parsed body reach a route handler?

Evidence

The key distinction is data representation: JSON and URL-encoded middleware expose parsed data through req.body, while raw middleware exposes a Buffer for the tested binary content type.

Sources: test/express.json.js:722-740, test/express.urlencoded.js:805-822, test/express.raw.js:65-83, test/express.raw.js:71-83

How static files are mounted

To serve files from a custom directory, create an application, pass the directory to the static helper, and mount the returned middleware with app.use. The static test helper accepts a directory argument, falls back to its fixture directory, and mounts that root before a fallback 404 handler.

function createApp (dir, options, fn) {
  var app = express()
  var root = dir || fixtures

  app.use(express.static(root, options))

  app.use(function (req, res, next) {
    res.sendStatus(404)
  })

  return app
}

The custom-directory form is app.use(express.static(root, options)): replace root with the directory to expose, or pass that directory as the first argument to the static helper. The supplied helper shows a second options argument but does not enumerate its fields.

The repository example uses path.join(__dirname, 'public') as the directory passed to the static helper. It explains that the middleware looks for a file matching req.path within that directory, so js/app.js is resolved under public/js/app.js in the example.

A mount path can add a URL prefix: the example describes static/js/app.js as a form where /static is removed before control reaches the static middleware. The excerpts establish the mounting pattern and path relationship, but they do not identify the npm package behind the static helper.

This workflow captures the custom-directory setup and its fallback.

Mounting a static directory — How does a custom directory become a static file endpoint?

Evidence

Sources: test/express.static.js:804-815, examples/static-files/index.js:7-22, examples/static-files/index.js:22-28, lib/express.js:15

How it connects

The application object is created by createApplication, which mixes in a prototype object named proto and initializes the app before returning it. Its request and response prototypes are built from the repository’s req and res modules, so middleware runs within the same application, request, and response model described by Express Overview and The Request Object.

Middleware is registered through app.use and participates in the application request-dispatch chain. The body-parser examples and static-file helper both show this registration shape. For broader ordering and dispatch rules, continue to App Routing & Request Dispatch and Routing Architecture.

Static serving is related to, but distinct from, dynamic file responses: the response documentation says res.sendFile() uses the same underlying code as the static middleware while serving a file dynamically from a route. See Response: Sending Data for that response-side path.

Sources: lib/express.js:36-56, lib/express.js:20, lib/express.js:21, test/express.json.js:722-740, test/express.static.js:804-815, lib/response.js:334-357

Key takeaways

  • Express exposes JSON, URL-encoded, raw, text, and static middleware helpers.
  • The shown implementation explicitly imports body-parser; the supplied source does not identify the package implementing static middleware.
  • URL-encoded and JSON middleware make route data available through req.body.
  • Raw middleware produces a Buffer for the tested application/octet-stream request.
  • Mount a custom directory with app.use(express.static(root, options)).

Want this for your repos?

Try Angada AI Wiki