Skip to content

andy-shea/junction-express-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Junction Express Middleware

Requirements: Node.js 6+

Express middleware to automatically setup/flush Junction pre/post request-response cycle

Install

yarn add junction-express-middleware

Example

import {junctionProvider, junctionFlush, setData} from 'junction-express-middleware';

app.use(junctionProvider(entityManager));
// or app.use(junctionProvider(entities, mapper)) to have the provider create the entityManager for you before returning the middleware

// ... add normal routes
async function someFunctionCalledInRoute(req, res, next) {
  try {
    const service = req.junction.get(FooService);
    const data = await service.asyncTaskReturningSomethingOfValue();
    setData(res, next, data);
  }
  catch(error) {
    next(error);
  }
}

// flush modifications and return JSON of whatever is stored in res.locals.data
// if res.locals.data is a function then it will return the result of that function call
app.use(junctionFlush);

Licence

MIT

About

Express middleware to automatically setup/flush junction pre/post request-response cycle

Resources

License

Stars

Watchers

Forks

Packages

No packages published