Flowstate Per-Request State Management

0.6.0 · abandoned · verified Wed Apr 22

Flowstate is a Node.js middleware designed for managing and propagating per-request state across a sequence of HTTP requests within an Express or Connect application. It allows developers to implement 'flows' where state is maintained across multiple redirects and user interactions, culminating in a desired outcome. By default, it integrates with existing session middleware, isolating state for specific request sequences rather than global session state. State propagation relies on `return_to` and `state` query/body parameters, which the middleware automatically handles on redirects and exposes via `res.locals` for manual inclusion in views. The current and only stable version is 0.6.0, published in 2017. The package appears to be abandoned, with no releases or significant activity since that time, making it potentially incompatible with modern Node.js and Express versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate flowstate middleware into an Express application, requiring `express-session` for state persistence. It shows how to initialize the middleware on a route, access per-request state via `req.state`, and manually propagate `return_to` and `state` parameters to a view using `res.locals` for subsequent requests.

const express = require('express');
const session = require('express-session');
const flowstate = require('flowstate');

const app = express();

// Basic session middleware is required for flowstate to persist data.
// In a real application, configure a robust session store.
app.use(session({
  secret: process.env.SESSION_SECRET || 'a-very-secret-key',
  resave: false,
  saveUninitialized: false,
  cookie: { secure: false } // Set to true in production with HTTPS
}));

// Set up a simple view engine (e.g., EJS for demonstration)
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');

app.get('/login', flowstate(), function(req, res, next) {
  // Access current state via req.state
  const messages = req.state.messages || [];
  res.locals.messages = messages;
  res.locals.hasMessages = !! messages.length;

  // Propagate return_to and state via res.locals for view rendering
  // if not already managed by flowstate itself (e.g., if a new state is created)
  res.locals.returnTo = res.locals.returnTo || req.query.return_to;
  res.locals.stateParam = res.locals.state || req.query.state;
  
  console.log('Current state handle:', res.locals.stateParam);
  console.log('Return to:', res.locals.returnTo);

  res.render('login', { 
    title: 'Login',
    // Example: Pass state information to the view
    flowstateHandle: res.locals.stateParam,
    returnTo: res.locals.returnTo
  });
});

// Dummy /views/login.ejs content for illustration:
// <form action="/login" method="POST">
//   <input type="hidden" name="return_to" value="<%= returnTo %>">
//   <input type="hidden" name="state" value="<%= flowstateHandle %>">
//   <button type="submit">Login</button>
// </form>

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
  console.log('Try visiting: http://localhost:3000/login?return_to=/dashboard');
});

view raw JSON →