Skip to content

Commit

Permalink
Set referrer when fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
armenzg committed Feb 16, 2018
1 parent 4c38f52 commit 56c1a91
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/fetch/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { createClient } from 'then-redis';
import fetch from 'node-fetch';
import moment from 'moment';

const os = require('os');

const defaultTtl = moment.duration(8, 'hours').as('seconds');

let db = null;
Expand All @@ -11,6 +13,8 @@ export default async function fetchText(
url,
{ ttl = defaultTtl, headers = {}, method = 'get' } = {},
) {
headers.referer = (process.env.NODE_ENV === 'development') ?
'localhost' : os.hostname();
const key = `cache:${url}`;
if (typeof ttl === 'string') {
ttl = moment.duration(1, ttl).as('seconds');
Expand All @@ -24,7 +28,8 @@ export default async function fetchText(
}
const response = await fetch(url, { method, headers });
if (!response.ok) {
// console.error(`Response for ${url} not OK: ${response.status}`);
console.error(`Response for ${url} not OK: ${response.status}`);
console.log(await response.text());
return null;
}
const text = await response.text();
Expand Down

0 comments on commit 56c1a91

Please sign in to comment.