Skip to main content

Express Transport

The @feathersjs/express module provides Express framework bindings and a REST API provider for Feathers applications.

Installation

npm install @feathersjs/express express

feathersExpress()

Creates a Feathers application that extends an Express app with Feathers functionality.

Signature

function feathersExpress<S = any, C = any>(
  feathersApp?: FeathersApplication<S, C>,
  expressApp?: Express
): Application<S, C>
feathersApp
FeathersApplication
A Feathers application instance to extend with Express functionality
expressApp
Express
An Express application instance (defaults to express())

Returns

Application
Application<S, C>
Combined Express and Feathers application with extended functionality including:
  • All Express app methods and middleware
  • All Feathers service and hook capabilities
  • Enhanced listen() method that calls Feathers setup()
  • Enhanced use() method that handles both Express middleware and Feathers services

Example

import feathers from '@feathersjs/feathers'
import express, { rest, json, urlencoded } from '@feathersjs/express'

const app = express(feathers())

// Express middleware
app.use(json())
app.use(urlencoded({ extended: true }))

// Feathers REST transport
app.configure(rest())

// Register services
app.use('/messages', messageService)

// Start server
app.listen(3030)

rest()

Configures the REST transport provider for Feathers services.

Signature

function rest(options?: RestOptions | RequestHandler): (app: Application) => void
options
RestOptions | RequestHandler
REST configuration options or a custom formatter function

RestOptions

formatter
RequestHandler
Custom response formatter middleware (defaults to JSON formatter)
authentication
AuthenticationSettings
Authentication configuration for parsing credentials from HTTP requests

Example

import express, { rest } from '@feathersjs/express'

const app = express(feathers())

// Use default REST provider
app.configure(rest())

// With custom formatter
app.configure(rest((req, res, next) => {
  res.format({
    'application/json': () => res.json(res.data)
  })
}))

// With authentication options
app.configure(rest({
  authentication: {
    service: 'authentication',
    strategies: ['jwt', 'local']
  }
}))

errorHandler()

Express error handling middleware that formats Feathers errors.

Signature

function errorHandler(options?: ErrorHandlerOptions): ErrorRequestHandler
options
ErrorHandlerOptions
Error handler configuration options

ErrorHandlerOptions

public
string
Path to public directory for HTML error pages
logger
boolean | { error?: Function, info?: Function }
Logger configuration for error logging
html
object | Function
HTML error page configuration or custom handler function
json
object | Function
JSON error response configuration or custom handler function

Example

import express, { errorHandler } from '@feathersjs/express'

const app = express(feathers())

// Use after all routes and services
app.use(errorHandler({
  logger: console,
  html: {
    404: 'path/to/404.html',
    500: 'path/to/500.html'
  }
}))

notFound()

Middleware that creates a NotFound error for unmatched routes.

Signature

function notFound(options?: { verbose?: boolean }): RequestHandler
options.verbose
boolean
default:"false"
Include the requested URL in the error message

Example

import express, { notFound, errorHandler } from '@feathersjs/express'

const app = express(feathers())

// Use before error handler
app.use(notFound())
app.use(errorHandler())

authenticate()

Express middleware for authenticating requests.

Signature

function authenticate(
  settings: string | AuthenticationSettings,
  ...strategies: string[]
): RequestHandler
settings
string | AuthenticationSettings
Authentication service name or settings object
strategies
string[]
Authentication strategies to use

AuthenticationSettings

service
string
Name of the authentication service
strategies
string[]
Array of authentication strategy names

Example

import express, { authenticate } from '@feathersjs/express'

// Protect specific routes
app.get('/protected', 
  authenticate('jwt'),
  (req, res) => {
    res.json({ message: 'Authenticated!' })
  }
)

// Protect a service
app.use('/messages', 
  authenticate('jwt'),
  messageService
)

parseAuthentication()

Middleware that parses authentication credentials from HTTP headers.

Signature

function parseAuthentication(settings?: AuthenticationSettings): RequestHandler
settings
AuthenticationSettings
Authentication parsing configuration

Example

import express, { parseAuthentication } from '@feathersjs/express'

const app = express(feathers())

// Automatically parse authentication from headers
app.use(parseAuthentication({
  strategies: ['jwt', 'api-key']
}))

Exported Express Utilities

The module also exports commonly used Express utilities:
import {
  json,           // JSON body parser
  urlencoded,     // URL-encoded body parser
  static,         // Static file serving
  Router,         // Express router
  cors,           // CORS middleware
  compression     // Response compression
} from '@feathersjs/express'

Service Options

Express-specific service options can be configured when registering services:
app.use('/messages', messageService, {
  express: {
    before: [/* middleware before service */],
    after: [/* middleware after service */]
  }
})
express.before
RequestHandler[]
Middleware to run before the service handler
express.after
RequestHandler[]
Middleware to run after the service handler

Request Extensions

The Express request object is extended with Feathers-specific properties:
req.feathers
Params
Feathers params object containing provider, headers, and other metadata
req.lookup
RouteLookup
Service route lookup information

Response Extensions

The Express response object is extended with:
res.data
any
The service method result data
res.hook
HookContext
The hook context from the service call

Build docs developers (and LLMs) love