Union Web Server Middleware

0.6.0 · abandoned · verified Wed Apr 22

Union is a Node.js middleware kernel designed to provide a hybrid buffered and streaming approach to handling HTTP requests and responses. It aims for backward compatibility with `connect` middlewares, allowing existing `connect` applications to leverage its features. The current stable version is 0.6.0, last published over two years ago as of early 2024. The package's `engines` configuration (`node >= 0.8.0`) further indicates its age and lack of recent maintenance. Key differentiators include its streaming middleware architecture, which avoids buffering entire request streams, and its integration with the Flatiron ecosystem, notably with `director` for routing. Unlike standard `connect` middlewares, `union`'s response object emits a 'next' event for control flow, a pattern used by Flatiron-specific middlewares but not directly compatible in reverse with `connect`'s `next()` callback style.

Common errors

Warnings

Install

Imports

Quickstart

This example sets up a basic Union server with `director` for routing. It demonstrates defining `before` middlewares, handling GET requests, and streaming POST request bodies to a file, showcasing Union's core streaming capabilities.

const fs = require('fs');
const union = require('union');
const director = require('director');

const router = new director.http.Router();

const server = union.createServer({
  before: [
    function (req, res) {
      const found = router.dispatch(req, res);
      if (!found) {
        res.emit('next'); // For Flatiron-compatible middlewares
      }
    }
  ]
});

router.get(/foo/, function () {
  this.res.writeHead(200, { 'Content-Type': 'text/plain' });
  this.res.end('hello world\n');
});

router.post(/foo/, { stream: true }, function () {
  const req = this.req;
  const res = this.res;
  const writeStream = fs.createWriteStream(Date.now() + '-foo.txt');

  req.pipe(writeStream);

  writeStream.on('close', function () {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Wrote request body to a stream!');
  });
});

server.listen(9090, () => {
  console.log('Union server with director running on port 9090');
});

// To run this example, ensure 'director' is installed: npm install director

view raw JSON →