-
Notifications
You must be signed in to change notification settings - Fork 0
headers
the headers object includes the following methods:
headers.all() will return a valid json object containing all received headers
router.get('/', function(stream, headers, flags){
// log all received headers
console.log(headers.all())
})
headers.get() will return a header from the headers object in nodejs http2 format
/**
* headers.get(key)
* @param {string} key // header name
**/
router.get('/', function(stream, headers, flags){
// return content-type header
console.log(headers.get('content-type'))
})
headers.is() will return a boolean if the header is equal to the comparison
/**
* headers.is(key, val)
* @param {string} key // header name
* @param {string} val // value to compare
**/
router.get('/admin', function(stream, headers, flags){
// check content-type
if(!headers.is('x-token', 'secret')){
app.blacklist(stream.ip)
}
})
headers.has() will return a boolean if the header exists
- will also return true for header that exists and has a value of false or 0
/**
* headers.has(key)
* @param {string} key // header name
**/
router.get('/', function(stream, headers, flags){
// check if cookie header exists
if(headers.has('cookie')){
console.log('cookie header exists')
}
})
headers.cookies() will return a deserialized cookies json object
router.get('/', function(stream, headers, flags){
// return cookies object
console.log(headers.cookies())
})
headers.ctype() will return the Content-type header if exists
router.get('/', function(stream, headers, flags){
console.log(headers.ctype())
// application/json ...
})
headers.agent() will return the User-agent header if exists
router.get('/', function(stream, headers, flags){
console.log(headers.agent())
// some browser user-agent ...
})
headers.bot() will return true if the user-agent detected is a bot
-
config.bot.items
should contain an array of bots to check for - this feature could be used to perform targeted seo optimization
router.get('/', function(stream, headers, flags){
if(headers.bot()){
// render template containing seo data only
let useragent = headers.ua();
if(useragent.includes('google')){
stream.render('index_seo_google.html', {
data: {
some: 'google data',
specifically: 'relating',
to: 'google seo'
}
})
} else if(useragent.includes('facebook')) {
stream.render('index_seo_facebook.html', {
data: {
some: 'facebook data',
specifically: 'relating',
to: 'facebook seo'
}
})
} else {
stream.render('index_seo_default.html', {
data: {
some: 'default data',
specifically: 'relating',
to: 'default seo'
}
})
}
} else {
// render normal template not polluted with seo
stream.render('index.html', {title: 'basic'})
}
})
headers.encoding() will return the accept-encoding header if exists
- the returned value/s will be within a trimmed array
router.get('/', function(stream, headers, flags){
console.log(headers.encoding())
// ['accepted', 'encoding']
})
headers.lang() will return the accept-language header if exists
- the returned value/s will be within a trimmed array
router.get('/', function(stream, headers, flags){
console.log(headers.lang())
// ['accepted', 'language']
})
headers.accept() will return the accept header if exists
- the returned value/s will be within a trimmed array
router.get('/', function(stream, headers, flags){
console.log(headers.accept())
// ['accepted', 'content', 'types']
})
headers.size() length of the headers object
router.get('/', function(stream, headers, flags){
let len = headers.size(); // headers length
if(len > 1000){
app.blacklist(stream.ip)
}
})
headers.count() will return a count of your total headers
router.get('/', function(stream, headers, flags){
let len = headers.count(); // headers count
if(len > 50){
app.blacklist(stream.ip)
}
})