Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bergos/set-link
Link header middleware for express with multiple value and attribute support
https://github.com/bergos/set-link
Last synced: about 2 months ago
JSON representation
Link header middleware for express with multiple value and attribute support
- Host: GitHub
- URL: https://github.com/bergos/set-link
- Owner: bergos
- Created: 2017-10-08T16:48:10.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-10-08T16:51:49.000Z (over 7 years ago)
- Last Synced: 2024-11-30T01:19:29.843Z (about 2 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# set-link
[![Build Status](https://travis-ci.org/bergos/set-link.svg?branch=master)](https://travis-ci.org/bergos/set-link)
[![npm version](https://badge.fury.io/js/set-link.svg)](https://badge.fury.io/js/set-link)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: ; 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: ; 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', '; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"')
res.setLink('http://example.org/context', 'http://www.w3.org/ns/json-ld#context')// Link: ; rel="http://www.w3.org/ns/hydra/core#apiDocumentation", ; rel="http://www.w3.org/ns/json-ld#context"
})
```