Skip to content

Instantly share code, notes, and snippets.

@alettieri
Created March 29, 2017 19:36
Show Gist options
  • Save alettieri/597a8c8930bcaef841e9cd29516f8fdc to your computer and use it in GitHub Desktop.
Save alettieri/597a8c8930bcaef841e9cd29516f8fdc to your computer and use it in GitHub Desktop.
HouseCanary api node proxy
// Request npm module - https://www.npmjs.com/package/request
const request = require('request');
const http = require('http');
const parse = require('url').parse;
// ===
// API Authentication
// Get test keys from: https://valuereport.housecanary.com/settings/api-settings
// ===
const API_KEY = '<REPLACE_WITH_API_KEY>';
const API_SECRET = '<REPLACE_WITH_API_SECRET>';
const options = {
baseUrl: 'https://api.housecanary.com/v2',
auth: {
user: API_KEY,
password: API_SECRET
}
};
function handleRequest (req, res) {
if (req.url === '/favicon.ico') {
res.end();
return;
}
options.url = parse(req.url).path;
request.get(options, handleResponse.bind(null, res));
}
function handleResponse (res, e, r, body) {
if (e) {
console.error(e);
res.writeHead(500);
res.end(e.message || 'Not sure what happened');
} else {
console.log('success', r.req.path);
const code = body.code || 200;
res.writeHead(code, {
'Content-Type': r.headers['content-type'],
'Content-Length': r.headers['content-length'],
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type'
});
res.end(body);
}
}
http.createServer(handleRequest).listen(9001);
@alettieri
Copy link
Author

  1. Replace the API Keys with your account API Keys
  2. run node proxy-server.js
  3. Server will be available under localhost:9001

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment