HTTP · In depth

HttpServer

Wraps Express with sensible defaults (JSON body parsing, CORS, and static asset folders) and starts listening immediately unless `listen: false` is supplied. You get the underlying Express instance back via `app`.

Explain it like I’m five

The simple mental model

Think of HttpServer as a furnished storefront: Express is the building, while Redweb installs the front door, service counter, signs, and sensible safety rails before you open.

When it fits

When should I use it?

Choose it when Redweb should own a normal HTTP listener and you still want direct access to Express for middleware or one-off routes.

A practical API pattern

Follow the example

This pattern explains the named API. Application classes, credentials, and assets may need to be supplied; use a complete recipe for a runnable starting point.

Read this article as Markdown

JavaScript
const { HttpServer, METHODS } = require('redweb')

const server = new HttpServer({
  port: 4000,
  bind: '0.0.0.0',
  publicPaths: ['./static'],
  services: [
    { serviceName: '/ping', method: METHODS.GET, function: (req, res) => res.json({ pong: true }) },
  ],
})

// Express is still available:
server.app.get('/health', (req, res) => res.send('ok'))
  1. 1

    Redweb creates the Express application and installs the configured parsers, CORS policy, static folders, and services.

  2. 2

    The server binds to the requested interface unless listen is false.

  3. 3

    The app property remains the same Express application, so adding /health does not require a Redweb abstraction.

Configuration

Choices you can make

  • port: number (default 80)
  • bind: string (default 0.0.0.0)
  • publicPaths: string[] (default ["./public"])
  • services: array of { serviceName, method, function }
  • listen: boolean (default true); set false to build app without binding a port
  • listenCallback: function invoked after listen
  • encoding: "json" | "urlencoded" (default json)
  • corsOptions: passed to cors
Surface area

Methods and members

constructor(options)

Merges defaults, wires body parsing, CORS, static serving, registers REST services, and starts listening unless listen is false.

app (Express instance)

Use the returned `app` to add middleware or routes exactly like a normal Express server.