Skip to content

bergos/set-link

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

set-link

Build Status npm version

Link header middleware for express with multiple value and attribute support.

Usage

The middleware doesn't require any parameters. Just add it to the express app like this:

const setLink = require('set-link')

const app = express()

app.use(setLink)

.setLink must be called with the link IRI and the relationship:

app.use((req, res) => {
  res.setLink('http://example.org/context', 'http://www.w3.org/ns/json-ld#context')

  // Link: <http://example.org/context>; rel="http://www.w3.org/ns/json-ld#context"
})

It's also possible to add additional attributes using a key value map:

app.use((req, res) => {
  res.setLink('http://example.org/context', 'http://www.w3.org/ns/json-ld#context', {
    title: 'example title',
    type: 'application/ld+json'
  })
  
  // Link: <http://example.org/context>; rel="http://www.w3.org/ns/json-ld#context"; title="example title"; type="application/ld+json"
})

If there is already a link header, the new one will be appended:

app.use((req, res, next) => {
  res.set('link', '<http://example.org/api>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"')
  res.setLink('http://example.org/context', 'http://www.w3.org/ns/json-ld#context')

  // Link: <http://example.org/api>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation", <http://example.org/context>; rel="http://www.w3.org/ns/json-ld#context"
})

About

Link header middleware for express with multiple value and attribute support

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published