Skip to content

About

seneca-web adapter for express

Resources

Code of conduct

Stars

11 stars

Watchers

7 watching

Forks

Repository files navigation

Seneca

A Seneca.js plugin

@seneca/web-adapter-express

The Express adapter for seneca-web: it registers the routes of a seneca-web route map on an Express app or router, turns each request into a Seneca message, and sends the action's reply back as the response, with optional authentication through passport. Works with Seneca 3 and Seneca 4 (prerelease 4.0.0-rc5 and later), seneca-web 2.x and Express 4 and 5; tested on Node.js 24 and 22.

npm version build

Voxgig This open source module is sponsored and supported by Voxgig.

Install

npm install @seneca/web-adapter-express seneca-web express seneca

Versions up to 1.2.1 were published as seneca-web-adapter-express; from the next version (1.3.0) the package is @seneca/web-adapter-express. seneca and seneca-web are peer dependencies.

Quick Example

const Seneca = require('seneca')
const SenecaWeb = require('seneca-web')
const Express = require('express')

const app = Express()

const seneca = Seneca({ log: 'warn' })
  .add('role:greeting,cmd:hello', function (msg, reply) {
    reply({ hello: msg.args.query.name || 'world' })
  })
  .use(SenecaWeb, {
    adapter: require('@seneca/web-adapter-express'),
    context: app,
    routes: {
      prefix: '/api',
      pin: 'role:greeting,cmd:*',
      map: { hello: true }, // GET /api/hello -> role:greeting,cmd:hello
    },
  })

seneca.ready(function () {
  app.listen(3000)
  // GET http://127.0.0.1:3000/api/hello?name=Seneca -> {"hello":"Seneca"}
})

The action receives the request data in msg.args (body, params, query, user, route) and the Express objects as msg.request$ and msg.response$; its reply is sent as JSON. Errors go to your Express error handlers.

More Examples

The full documentation index is docs/README.md.

Motivation

seneca-web describes which URLs reach which Seneca actions; this adapter does the Express part. It registers the routes, builds a message from each request and turns the reply into the response, so actions stay plain Seneca actions that know nothing about routing. See How seneca-web adapters work and Express specifics.

Support

API

The module exports the adapter function, which seneca-web calls; see the Adapter contract.

Adapter options (options in the seneca-web plugin options), see Options:

Option Default Purpose
parseBody true Read the raw request body into msg.args.body as a string. Set false when a body parser is installed.
includeRequest true Attach the Express request as msg.request$.
includeResponse true Attach the Express response as msg.response$.
middleware none Named middleware that routes refer to by name.

The message for each request, see Message payload:

Property Value
args.body Raw body string, or req.body with parseBody: false.
args.params, args.query req.params, req.query.
args.user req.user, or null.
args.route The route object from seneca-web.
request$, response$ The Express request and response.

What each outcome becomes in HTTP (errors, redirect, autoreply, auth, secure), see Response handling. Every option, property and error is listed in the feature index.

Contributing

The Senecajs org encourages open participation. If you feel you can help in any way, be it with documentation, examples, extra testing, or new features please get in touch.

To run the tests (ESLint, then Mocha) on Node.js 24 or 22:

npm install
npm test

The tests run against the Seneca and Express versions in devDependencies (seneca@^4.0.0-rc5, the Seneca 4 prerelease, and express@^5.1.0). To test against other versions, install them without saving, for example npm install --no-save seneca@3 express@4, then npm test; npm install restores the defaults. npm run coverage writes a coverage report to coverage/. The examples run with node docs/examples/<name>.js and exit on their own.

The GitHub Actions workflow for continuous integration is provided as a patch in .patches/, because adding workflow files needs a GitHub token with the workflow scope; apply it with git am .patches/*.patch.

Background

The adapter was created in 2016 (version 1.0.0) from the Express support in seneca-web, when seneca-web moved framework code into separate adapter packages. Middleware support came in 1.1.0 and the includeRequest and includeResponse options in 1.2.0. Version 1.3.0 adds Seneca 4 support and sends route messages from the root instance, so that they no longer inherit fatal$ from plugin loading or seneca-web's plugin$. It was published as seneca-web-adapter-express up to 1.2.1, and is published as @seneca/web-adapter-express from the next version. The change log is in CHANGES.md.

Adapter Seneca seneca-web Express Node.js
1.3.x 3.x and 4.x (from 4.0.0-rc5) 2.x 4 and 5 22 and later; tested on 24 and 22
1.2.x 2.x and 3.x 1.x and 2.x 4 8 to 12

Licensed under MIT.

About

seneca-web adapter for express

Resources

Code of conduct

Stars

11 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages