0.1.18 • Published 4 years ago

@mgfx/express-middleware v0.1.18

Weekly downloads
76
License
ISC
Repository
github
Last release
4 years ago

@mgfx/express-middleware

Express middleware for integration with mgFx applications.

Features

  • Creates per-route mgFx contexts that includes http.method and http.path Context Values automatically
  • Run mgFx processes in that Context via req.mgFx.run()
  • Run an arbitrary Future and fork it's resolution value or rejection reason to the HTTP response via req.mgFx.fork
  • Run a Task and fork it to the Response in a single shot via req.mgFx.runF()

Usage

  1. Install @mgfx/express-middleware into your existing mgFx/Express application:
$ yarn add @mgfx/express-middleware
  1. Add the Middleware to an Express application:
import express from 'express';
import { localConnector } from 'mgfx';
import { expressMiddleware } from '@mgfx/express-middleware';

const connector = localConnector();
const app = express();

app.use(expressMiddleware({ connector }));

/** mgFx Task definitions, Express routes, etc */
  1. Use req.mgFx within your Express handlers:
import { after, fork, map } from 'fluture';

// Use `req.mgFx.run` to run a Task within the context of the current HTTP request:
app.get('/run/:name', (req, res, next) => {
  // `myTask` will be invoked in a Context that contains the values:
  // { 'http.path': '/run/:name', 'http.method': 'GET' }
  req.mgFx
    .run(myTask({ name: req.params.name }))
    // Explicitly use `Fluture.fork` to run this Future
    .pipe(fork(error => next(error)(result => res.send(result))));
});

// Use `req.mgFx.fork` to run an arbitrary Future and send it's value/error to the Response:
app.get('/fork/:name', req => {
  after(100)(`Hello ${name}!`).pipe(req.mgFx.fork);
});

// Use `req.mgFx.runF` to run a Task and send it's output directly to the response:
app.get('/runF/:name', req => {
  req.mgFx.runF(myTask({ name: req.params.name }));
});

// Use `req.mgFx.run` and `req.mgFx.fork` to run a Task, do some intermediate operation, and send the result to the
// Response:
app.get('/complex/:name', req => {
  req.mgFx
    .run(myTask({ name: req.params.name }))
    .pipe(map(result => result.length))
    .pipe(req.mgFx.fork);
});
0.1.35

4 years ago

0.1.36

4 years ago

0.1.37

4 years ago

0.1.34

4 years ago

0.1.33

4 years ago

0.1.32

4 years ago

0.1.31

4 years ago

0.1.30

4 years ago

0.1.29

4 years ago

0.1.28

4 years ago

0.1.27

4 years ago

0.1.26

4 years ago

0.1.25

4 years ago

0.1.24

4 years ago

0.1.23

4 years ago

0.1.22

4 years ago

0.1.21

4 years ago

0.1.20

4 years ago

0.1.19

4 years ago

0.1.18

4 years ago

0.1.15

4 years ago

0.1.16

4 years ago

0.1.17

4 years ago

0.1.13

4 years ago

0.1.14

4 years ago

0.1.12

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.4

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago