Skip to content

Commit 487f7df

Browse files
committed
update pkg to support both vanilla node and express
1 parent a32fc70 commit 487f7df

File tree

7 files changed

+91
-36
lines changed

7 files changed

+91
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
yarn.lock
44
yarn-error.log
55
package-lock.json
6+
.idea/

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
yarn.lock
44
yarn-error.log
55
package-lock.json
6+
.idea/

README.MD

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Node/Express GNU Clacks
33

4-
A man is not dead while his name is still spoken. Add an 'X-Clacks-Overhead' Header to your Node/Express app.
4+
A man is not dead while his name is still spoken. Add an 'X-Clacks-Overhead' Header to your vanilla Node or Express app.
55

66
See [http://www.gnuterrypratchett.com](http://www.gnuterrypratchett.com "GNU Terry Pratchett")
77

@@ -16,22 +16,50 @@ npm install node-gnu-clacks --save
1616
```
1717

1818
## Usage
19-
- Import the module and tell your app to use as middleware.
19+
- Import the module and tell your app to use as middleware:
20+
21+
```js
22+
const { node: gnuHeaderNode } = require('node-gnu-clacks');
23+
const { express: gnuHeaderExpress } = require('node-gnu-clacks');
24+
```
25+
26+
_note: you can also use `const gnuHeader = require('node-gnu-clacks');` for express apps for backwards compatibility with v1._
2027

2128
## Options
22-
- Optionally, add an array of names to be passed along with the default, 'Terry Pratchett'.
29+
**string[] ghosts = ['Terry Pratchett']**
30+
31+
Add an array of names to be passed. This is in addition to the default, 'Terry Pratchett', which will always be added.
2332

24-
## Example
33+
## Example with a vanilla Node app
2534

2635
```js
36+
const http = require('http');
37+
const { node: gnuHeaderNode } = require('node-gnu-clacks');
2738

28-
const express = require('express');
29-
const gnuHeader = require('node-gnu-clacks');
39+
http.createServer((req, res) => {
40+
const addClacksHeader = gnuHeaderNode(['Moist', 'Adora', 'Reacher']);
41+
addClacksHeader(res);
42+
43+
res.writeHead(200, {'Content-Type': 'text/plain'});
44+
res.end('A man is not dead while his name is still spoken.\n');
45+
}).listen(3000);
46+
```
47+
48+
## Example with an Express app
49+
50+
```js
51+
const express = require('lib/express');
52+
const { express: gnuHeaderExpress } = require('node-gnu-clacks');
3053

3154
const app = express();
3255

33-
app.use(gnuHeader());
56+
app.use(gnuHeaderExpress());
3457
or
35-
app.use(gnuHeader(['Moist', 'Adora', 'Reacher']));
58+
app.use(gnuHeaderExpress(['Moist', 'Adora', 'Reacher']));
59+
60+
app.get('/', (req, res) => {
61+
res.send('A man is not dead while his name is still spoken.');
62+
});
3663

64+
app.listen(3000);
3765
```

index.js

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
1-
/**
2-
* GNU-clacks Node/Express middleware.
3-
*
4-
* - `ghosts`: provide an array of (string) names to be transmitted through the clacks (default: Terry Pratchett)
5-
*
6-
* @param {string} ghosts
7-
* @return {function}
8-
*/
1+
// Main entry point to export both Node and Express versions
2+
const expressVersion = require('./lib/express');
3+
const nodeVersion = require('./lib/node');
94

10-
module.exports = function (ghosts) {
11-
ghosts = ghosts || [];
5+
// default export
6+
module.exports = expressVersion;
127

13-
if (!Array.isArray(ghosts)) return function (req, res, next) {
14-
res.setHeader('X-Clacks-Overhead', 'GNU Terry Pratchett');
15-
next()
16-
}
17-
else {
18-
ghosts.unshift('GNU Terry Pratchett');
19-
ghosts = ghosts.join(', ').toString();
20-
21-
return function (req, res, next) {
22-
if (ghosts.length > 0) {
23-
res.setHeader('X-Clacks-Overhead', ghosts);
24-
}
25-
next();
26-
}
27-
}
28-
29-
};
8+
// named exports
9+
module.exports.express = expressVersion;
10+
module.exports.node = nodeVersion;

lib/express.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* GNU-clacks Express middleware.
3+
*
4+
* - `ghosts`: provide an array of (string) names to be transmitted through the clacks
5+
* (default: ['Terry Pratchett'] if not provided or input is not an array).
6+
*
7+
* @param {string[]} [ghosts=[]] - An optional array of names to be transmitted.
8+
* Defaults to an empty array, which will be replaced
9+
* by ['Terry Pratchett'] in the implementation.
10+
* @return {function} Middleware function for use in Express.js applications.
11+
*/
12+
13+
module.exports = function (ghosts = []) {
14+
ghosts = Array.isArray(ghosts) ? ghosts : [ghosts];
15+
16+
const clacksValue = ['GNU Terry Pratchett', ...ghosts.map(name => `GNU ${name}`)].join(', ');
17+
18+
return function (req, res, next) {
19+
res.setHeader('X-Clacks-Overhead', clacksValue);
20+
next();
21+
};
22+
};

lib/node.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Sets the X-Clacks-Overhead HTTP header for Node.js HTTP responses.
3+
*
4+
* - `ghosts`: An optional array of string names to be transmitted through the clacks.
5+
* If not provided or if the input is not an array, defaults to ['Terry Pratchett'].
6+
*
7+
* @param {string[]} [ghosts=[]] - An optional array of names to be transmitted.
8+
* Defaults to an empty array, which implies that 'GNU Terry Pratchett'
9+
* will be added by default.
10+
* @return {function(http.ServerResponse)} - A function that takes a Node.js HTTP ServerResponse object
11+
* and sets the X-Clacks-Overhead header on it.
12+
*/
13+
14+
module.exports = function (ghosts = []) {
15+
ghosts = Array.isArray(ghosts) ? ghosts : [ghosts];
16+
17+
const clacksValue = ['GNU Terry Pratchett', ...ghosts.map(name => `GNU ${name}`)].join(', ');
18+
19+
return function (response) {
20+
response.setHeader('X-Clacks-Overhead', clacksValue);
21+
}
22+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"clacks",
1111
"terry pratchett"
1212
],
13-
"main": "index.js",
13+
"main": "lib/express.js",
1414
"scripts": {
1515
"test": "echo \"Error: no test specified\" && exit 1"
1616
},

0 commit comments

Comments
 (0)