Giter VIP home page Giter VIP logo

epoxy's Introduction

🪣 Epoxy

npm version minzip size

Simple server-side per-route html injection

Purpose

Epoxy allows for running middleware on select HTTP routes that inject HTML into the served content. Simply point it at a static directory, define some routes in a script and you're off to the races! 🐴

Epoxy comes with esbuild, which is used for transpiling a route handler file.

Usage

Add as a dependency

yarn add @stevent-team/epoxy

Options

You can also run npx epoxy --help to see this information.

epoxy

Usage: epoxy <target> [routeFile] [options]

Serve the provided static folder

Arguments:
  target                 Path to static directory
  routeFile              Path to cjs router script (can use ES6 with --build option)

Options:
  -V, --version          output the version number
  -p, --port <port>      port to use for http server (default: 8080)
  -h, --host <url>       host to use for http server (default: "0.0.0.0")
  -i, --index <path>     path to index html inside of target (default: "index.html")
  -b, --build            build routes file in memory (default: false)
  --no-cache             disable all route handler result caching
  --help                 display help for command

epoxy build

Usage: epoxy build <routeFile> [options]

Build a routes file

Arguments:
  routeFile                Path to ES6 router script

Options:
  -o, --outputDir <path>   folder to output built routes file (default: "dist")
  -h, --help               display help for command

Example

Create a script to define your dynamic routes.

// Handlers are async functions returning HTML to be injected
const routeHandler = async req => {
  const { params } = req // Get route params from request

  return {
    head: `<meta property="coolness" value="${params.coolness}">`, // Injected into the end of the <head>
    body: 'Howdy!' // Injected into the end of the <body>
  }
}

export default {
  '/some/route/with/:coolness': routeHandler
}
See an example using the `createMeta` helper

createMeta Helper

Epoxy comes with a helper that allows you to easily create meta tags from an object.

import { createMeta } from '@stevent-team/epoxy/helpers'

const routeHandler = async ({ params }) => {
  // Create a string of meta tags from the object passed in
  const metaTags = createMeta({
    coolness: params.coolness,
    description: 'A pretty cool page',
  })

  return { head: metaTags }
}

export default {
  '/route/:coolness': routeHandler
}

For more information about the helpers available in Epoxy, see the readme.

Then serve your static directory and dynamic routes!

epoxy ./dist ./routes.js --build

or setup an npm script

// package.json
{
  "scripts": {
    "serve": "epoxy ./dist ./routes.js --build"
  }
}

If you have a deployment that will need to start and stop your Epoxy server often, such as an automatically scaled deployment like Google App Engine, then you can prebuild the routes file so it doesn't have to build before every start:

// package.json
{
  "scripts": {
    "build": "epoxy build ./routes.js",
    "serve": "epoxy ./dist ./dist/routes.js"
  }
}

Alternatively, you could also write your routes file in CommonJS so it doesn't require building; the epoxy command only builds if the flag --build is specified.

API

Your route handler will be built by Epoxy using Parcel when Epoxy is started. It also has to export a default object with the structure:

export default {
  'express/js/route/:withParams': yourRouteHandlerFunction
}

// or

export default {
  'express/js/route/:withParams': {
    handler: yourRouteHandlerFunction,
    key: request => ['A key to cache with', request.params.withParams], // optional, any type, will cache result based on key
    ttl: 3600000, // time to live, optional, in milliseconds
  }
}

If you include a key that is not undefined, then the result of the handler function will be cached based on that key. You can also include a time to live ttl parameter which will expire the cache after a certain amount of milliseconds. There is also a command line argument --no-cache that will disable all caching irrespective of any keys provided.

Each route must have a function to handle it, which will receive a request object from ExpressJS, from which you can learn about the request. See the express docs for the request object for more information.

Contributing

PRs and Issues are more than welcome :)

This library uses changesets. If the changes you've made would constitute a version bump, run yarn changeset and follow the prompts to document the changes you've made. Changesets are consumed on releases, and used to generate a changelog and bump version number.

License

Created by Stevent (2022) and licensed under MIT

epoxy's People

Contributors

giraugh avatar github-actions[bot] avatar gra0007 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

tdib

epoxy's Issues

Allow Epoxy route files to be built before deployment of a frontend app

Currently the routes files are being build in memory at runtime, which can slow down automatically scaled deployments. We should support an epoxy build command that builds the routes file ahead of time, and with that an epoxy serve --build command that will maintain the current functionality for workflows that can't depend on a built routes file.

Investigate using esbuild

Currently Epoxy ships with Parcel, which it uses to build the routing config file. We should investigate using esbuild for a faster experience with less overhead.

Caching

Epoxy should cache routes that are visited to avoid repeated database calls for the same route in a short time frame.

Use fastify instead of express

According to these benchmarks, fastify is more than 3 times faster than express. Due to the simple involvement of a web server in this library, fastify should be investigated as a drop-in replacement for _blazing fast_™️⚡️ performance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.