Skip to content

Arthelon/spf-express

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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