Git Product home page Git Product logo

spf-express's Introduction

spf-express npm

Express middleware for youtube's SPF framework.

Framework documentation can be found here

Features

  • Intercept requests with custom SPF query identifiers
  • Attach SPF response objects or custom express handler to desired path

Installation

npm install spf-express

To run tests:

npm test

API

spf_express(opts)   //returns an express middleware function

Options:

  • paths: Object
    • [path: String]: Object | Function
    • Key value can either be a SPF response object or an express middleware function (supports URL params)
    • Will return the corresponding response object or forward request to middleware when a SPF request is sent to a defined path.
  • identifiers: [String]
    • default: ["navigate", "prefetch", "navigate-back"]
    • Extends on the default valid SPF request identifiers on the "spf" query field
      • if the "spf" query field in a request contains a value in this field, it will be treated as a valid SPF request.
  • override: Boolean
    • default: False
    • will override default list of SPF request identifiers with those set in the "identifiers" field if set to True.

Examples

Simple example

var spf_express = require("spf-express"),
    express = require("express"),
    app = express()

app.use(spf_express({
    paths: {
        "/sample_path": {
            title: "My website title",
            body: "<h1>Hello World!</h1>"
        } //SPF response object
    }
}))

More on SPF response objects here

Advanced example

app.use(spf_express({
    paths: {
        "/sample_path": function(req, res, next) { //express middleware function
            res.json({
                title: "My website title",
                body: "<h1>Hello World!</h1>"
            })
        }   
    },
    identifiers: ["my_identifier"],
    override: true   //"my_identifier" is now the only valid SPF request identifier!
}))

Example project

$ git clone https://github.com/Arthelon/spf-express.git
$ cd spf-express
$ npm install --dev
$ npm run start-example

spf-express's People

Contributors

andrewfluck avatar arthelon avatar newtsrock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

andrewfluck

spf-express's Issues

Please fix the docs

The documentation for this is horrible, can you write a better one, maybe even make an example in this repo so I could better understand how this works. This is just what I need, I just don't understand how to use it. Thank you ๐Ÿ˜„

No support for URL params in express middleware functions

If I do something like

paths: {
    '/weather/:zip': (req, res, next) {
        res.json({
            body: {
                content: ejs.renderFile('./views/partials/weather.ejs', {
                    title: `Weather (${req.params.zip})`
                }, (err, str) => {
                    if (err) return err;
                    return `${str}`;
                })
            }
        })
    }
}

It will redirect as normal instead of replacing content.
Same story for query strings.
@Arthelon please fix.

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.