Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@

hapi will automatically generate ETag headers for your responses when you use the file handler or `reply.file()` method. But if you're using any other kind of response (such as JSON, HTML, text etc) you won't get ETags for free. This plugin fixes that!

##Installation and configuration
## Installation and configuration

To install, just add to your npm deps:

```shell
npm install --save hapi-etags
```

Then register the plugin:

server.register([
{
register: require('hapi-etags'),
options: {
// explained below
}
}
], function (err) {

if (err) {
throw err;
}

server.start(function () {
console.log('Started!');
});

});
```javascript
server
.register({
plugin : require('hapi-etags'),
options: {
// explained below
}
})
.then(() => {
server.start().then(() => {
console.log('Started!');
});
});
```

The following options are available when registering the plugin [defaults]:

Expand All @@ -37,7 +35,7 @@ The following options are available when registering the plugin [defaults]:
* `varieties` - A list of the variety types that the plugin will calculate etags for. Options are `['plain', 'buffer', 'view', 'stream']`. Default: `['plain', 'buffer']`
* `etagOptions` - The same options argument that's passed to `response.etag` (http://hapijs.com/api#response-object-redirect-methods). Default: `{}`

##Advice and warnings
## Advice and warnings

Only the `plain` and `buffer` varieties are set by default. Support for the other varieties should be considered experimental. Here's some issues to be aware of:

Expand Down
258 changes: 131 additions & 127 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -1,151 +1,155 @@
var Hapi = require('hapi');
const Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: 4000 });
const server = new Hapi.Server({
port: 4000
});

server.views({
engines: {
hbs: require('handlebars')
server.register({
plugin: require('vision')
}).then(() => {
server.views({
engines : {
hbs: require('handlebars')
},
relativeTo: __dirname
});
});

server.route([
{
method: 'GET',
path: '/object',
handler: function (request, reply) {

var users = [
{
gender: 'female',
name: {
title: 'ms',
first: 'manuela',
last: 'velasco'
},
location: {
street: '1969 calle de alberto aguilera',
city: 'la coruña',
state: 'asturias',
zip: '56298'
}
}
];

reply(users);
},
config: {
cache: {
privacy: 'private',
expiresIn: 86400 * 1000
}
}
}, {
method: 'GET',
path: '/string',
handler: function (request, reply) {

reply('This is a string');
},
config: {
cache: {
privacy: 'private',
expiresIn: 86400 * 1000
}
}
}, {
method: 'GET',
path: '/number',
handler: function (request, reply) {

reply(42);
},
config: {
cache: {
privacy: 'private',
expiresIn: 86400 * 1000
}
{
method : 'GET',
path : '/object',
handler: function (request, h) {

const users = [
{
gender : 'female',
name : {
title: 'ms',
first: 'manuela',
last : 'velasco'
},
location: {
street: '1969 calle de alberto aguilera',
city : 'la coruña',
state : 'asturias',
zip : '56298'
}
}
}, {
method: 'GET',
path: '/buffer',
handler: function (request, reply) {

reply(new Buffer('I am a buffer!'));
},
config: {
cache: {
privacy: 'private',
expiresIn: 86400 * 1000
}
}
}, {
method: 'GET',
path: '/view',
handler: function (request, reply) {

reply.view('index', {data: 'somethins'});
},
config: {
cache: {
privacy: 'private',
expiresIn: 86400 * 1000
}
}
}, {
method: 'GET',
path: '/stream',
handler: function (request, reply) {
];

var stream = new (require('stream').Readable);
return users;
},
config : {
cache: {
privacy : 'private',
expiresIn: 86400 * 1000
}
}
}, {
method : 'GET',
path : '/string',
handler: function (request, h) {

var i = 0;
return 'This is a string';
},
config : {
cache: {
privacy : 'private',
expiresIn: 86400 * 1000
}
}
}, {
method : 'GET',
path : '/number',
handler: function (request, h) {

return 42;
},
config : {
cache: {
privacy : 'private',
expiresIn: 86400 * 1000
}
}
}, {
method : 'GET',
path : '/buffer',
handler: function (request, h) {
const buf = Buffer.from('I am a buffer!');
return h.response(buf).bytes(buf.length).code(200);
},
config : {
cache: {
privacy : 'private',
expiresIn: 86400 * 1000
}
}
}, {
method : 'GET',
path : '/view',
handler: function (request, h) {

stream._read = function () {
return h.view('index', { data: 'something' });
},
config : {
cache: {
privacy : 'private',
expiresIn: 86400 * 1000
}
}
}, {
method : 'GET',
path : '/stream',
handler: function (request, h) {

var self = this;
const stream = new (require('stream').Readable);

if (i === 20) {
return this.push(null);
}
let i = 0;

setTimeout(function (){
self.push(i.toString());
i++
}, 100);
};
stream._read = function () {

var res = reply(stream);
const self = this;

res.header('content-type', 'text/html');
},
config: {
cache: {
privacy: 'private',
expiresIn: 86400 * 1000
}
if (i === 20) {
return this.push(null);
}
}
]);

server.register([
{
register: require('..'),
options: {
encoding: 'hex',
algo: 'md5',
varieties: ['plain', 'buffer', 'view', 'stream']
}
}
], function (err) {
setTimeout(function () {
self.push(i.toString());
i++
}, 100);
};

if (err) {
throw err;
return h
.response(stream)
.header('content-type', 'text/html');
},
config : {
cache: {
privacy : 'private',
expiresIn: 86400 * 1000
}
}
}
]);

server.start(function () {
try {
server
.register({
plugin : require('..'),
options: {
encoding : 'hex',
algo : 'md5',
varieties: ['plain', 'buffer', 'view', 'stream']
}
})
.then(() => {
server.start().then(() => {
console.log('Started!');
});
});
} catch (err) {
throw err;
}

});
Loading