Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including syncano-middleware with all npm packages installed. Try it out:

var syncanoMiddleware = require("syncano-middleware")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

syncano-middleware v0.1.2

Middleware provide a convenient mechanism for filtering HTTP requests entering your socket.

XO code style CircleCI codecov

Syncano Middleware

Syncano middleware provide a convenient mechanism for filtering HTTP requests entering your socket. For example validate passed parameters, check if user is authenticated, assign default parameter values and many more.

For full list of middlewares check NPM.

Getting started

Installing from NPM

npm install syncano-middleware --save

Usage

import middleware from 'syncano-middleware';

// Example middlewares.
const steps = {
  auth: () => new Promise((resolve, reject) => {
    // Your user authentication logic...
    resolve(user)
  }),
  validate: () => new Promise((resolve, reject) => {
    // Your validation logic...
    resolve(formFields)
  })
}

middleware([ steps.auth, steps.validate ])
  .then(data => {
    // All middlewares passed.
    // Now you have an authenticated user and validated form.

    const [ user, fields ] = data
  })
  .const(err => {
    // An error occurred in one of middlewares.
    // You can handle that here.
  })


Custom middlewares

Middleware must be a function that returns a Promise:

  function MyAwesomeMiddleware() {
    return new Promise((resolve, reject) => {
      // Your awesome middleware implmentation...
      // ...
      // resolve(data)
      // or
      // reject('An error message or data')
    })
  }
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free