@transomjs/transom-scaffold

Serve static assets and add redirect routes to a Transom REST API.

Usage no npm install needed!

<script type="module">
  import transomjsTransomScaffold from 'https://cdn.skypack.dev/@transomjs/transom-scaffold';
</script>

README

Transom Scaffold

Add routes for static assets, redirects and template based content to your Transom server.

Build Status Coverage Status

Installation

$ npm install --save @transom/transom-scaffold

Usage

const myApi = require('./myApi');

const Transom = require('@transomjs/transom-core');
const transomScaffold = require('@transomjs/transom-scaffold');
const transom = new Transom();

transom.configure(transomScaffold, {
  scaffold: {
    staticRoutes: [{
      path: /(js|css|fonts|images)\/?.*/,
      folder: 'static-assets'
    }],
    redirectRoutes: [{
        path: '/',
        target: '/login'
      },
      {
        path: '/dummy',
        target: '/login'
      }
    ],
    person: {
      templateName: "Person",
      data: {
        pageTitle: "Person",
        appName: 'Transom Scaffold Demo'
      }
    }  
  }
});

const server = transom.initialize(myApi);

Scaffold options

The options can be passed in to the configure as above, or the scaffold object can be created as a property of the definition object in your Transom configuration. There are two named attributes of scaffold that have special meaning.

staticRoutes

staticRoutes is an Array of Objects used to create GET routes to serve static content. Each entry must include a path and a folder in the application root where static content can be stored. The path can be a regex, or a string and is used to create a new route to files stored in the specified folder. The URL path is combined with the folder path to allow further organizing of static resources.

  scaffold: {
    staticRoutes: [{
      path: /(js|fonts|images)\/?.*/,
      folder: 'static-assets'
    }],
  }

Translates to routes which serve the following files:

  • http://localhost:7000/fonts/comic-sans.ttf
     ⇨  /static-assets/fonts/comic-sans.ttf

  • http://localhost:7000/images/logo.png
     ⇨  /static-assets/images/logo.png

  • http://localhost:7000/js/bootstrap-select/js/bootstrap-select.js
     ⇨  /static-assets/js/bootstrap-select/js/bootstrap-select.js

redirectRoutes

redirectRoutes is an Array of Objects used to create GET routes that redirect to the target. Each entry must include a path which is used to create routes that redirect to the location specified by the target. Target follows the options spec as defined in the Restify.redirect documentation.

  scaffold: {
    redirectRoutes: [{
      path: '/',
      target: '/login'
    },
    {
      path: '/dummy',
      target: {
        pathname: '/foo/bar',
        permanent: true
      }
    }]
  }

Translates to routes which redirect to the following URLs:

  • http://localhost:7000  ⇨  http://localhost:7000/login
  • http://localhost:7000/dummy  ⇨  http://localhost:7000/foobar

Additional Properties

Each additional property of scaffold is used to create a GET route that renders the template identified by templateName. Scaffold uses a template handler such as the one provided by TransomEJSTemplate to load the template and feed it the content of the data attribute.

  scaffold: {
    about: {
      templateName: "NewUserVerify",
        data: {
          pageTitle: "Verify",
          appName: 'Transom Scaffold Demo',
          verifyUrl: '/api/v2/user/verify'
        }
    }
  }

Translates to a route at http://localhost:7000/about that responds with a templated web page, including a title of Verify, an App name of Transom Scaffold Demo etc.

Examples that use this plugin