JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q52760F
  • License MIT

Package Exports

  • express-bff
  • express-bff/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (express-bff) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

An Express middleware for simple way to create BFF (backend-for-frontend) server for your RIA/SPA applications.

Features

  • Automated client-side security.
  • Built-in sessions.
  • File-based REST API controllers with automatic route-matching.
  • Server-sent event (SSE) support out-of-the-box.
  • Automated proxying of requests to backend server.
  • Easy integration with Server-side rendering (SSR) engines.
  • Static files serving if needed.
  • Query to SQL (QSQL) support. (coming soon)
  • Memoization of response based on request params. (coming soon)
  • GrapthQL integration. (coming soon)
  • Websockets support. (coming soon)

Install

npm i express-bff --save

Usage

Configuration

const path = require('path');
const express = require('express');
const bff = require('express-bff');

const app = express();

bff(app, {
    security: {
        cors: false,
        secure,
    },
    session: {
        persist: true,
        // OR
        persist({ secret, ttl }) {
          return new CustomStore();
        }
    },
    sse: {
        path: '/events',
        // option serializer callback for objects
        serializer(key, val) {
          return val;
        }
    },
    api: {
        dir: path.join(__dirname, 'routes'),
    },
    proxy: {
        target: 'https://192.x.x.x:3030',
    },
    static: {
        dir: path.join(__dirname, 'static'),
        single: true,
        dev,
    },
    ssr: {
      template: 'index', // provide template name for express render engine
      handler(req) {
        return { /* any locals to render in template */ };
      }
    }
});

app.listen(process.env.PORT);

REST API controller

// ./routes/posts/index.js => /posts

module.exports = {
    get,
    post,
    patch,
    put,
    delete: del
};

async function get(req, res) {
  // ...
}

async function post(req, res) {
  // ...
}

async function put(req, res) {
  // ...
}

async function patch(req, res) {
  // ...
}

async function del(req, res) {
  // ...
}

Route with dynamic parameters:

// ./routes/posts/:id.js => /posts/:id

module.exports = {
    get,
    ...
};

async function get(req, res) {
  console.log('ID is ', req.params.id);
}