How can I get the referrer from the request object? The variable this.request.headers['referer']
was empty.
If your page was referred by another page, the referer will be accessable through this.headers.referer
.
If the page was not refered by another page (was loaded directly), this.headers.referer
will be undefined.
This demo code:
'use strict'
const Koa = require('koa')
let app = new Koa()
app.use(function * () {
console.log(this.headers)
})
app.listen(8888)
Yielded this when referred by another page:
{ host: 'localhost:8888',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-language': 'en-US,en;q=0.5',
'accept-encoding': 'gzip, deflate',
referer: 'http://localhost:1111/',
connection: 'keep-alive' }
And this when loaded directly:
{ host: 'localhost:8888',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'accept-language': 'en-US,en;q=0.5',
'accept-encoding': 'gzip, deflate',
connection: 'keep-alive' }