Created
March 29, 2017 19:36
-
-
Save alettieri/597a8c8930bcaef841e9cd29516f8fdc to your computer and use it in GitHub Desktop.
HouseCanary api node proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
node proxy-server.js