Skip to content

Instantly share code, notes, and snippets.

@dmfenton
Created February 23, 2017 21:28
Show Gist options
  • Save dmfenton/066061daa62b53c60f1fcbf94ade9567 to your computer and use it in GitHub Desktop.
Save dmfenton/066061daa62b53c60f1fcbf94ade9567 to your computer and use it in GitHub Desktop.
/*
Example request: http://koop.com/craigslist/washingtondc/apartments/FeatureServer/0/query?where=price>20&f=geojson
Req is the express request object: https://expressjs.com/en/4x/api.html#req
req.params = {
host: 'washingtondc',
id: 'apartments'
}
req.query = {
where: 'price > 20',
f: geojson
}
*/
function getData(req, callback) {
// pull pieces we need to form a request to Craigslist from the req.params object.
const city = req.params.host
const type = req.params.id
// using the npm package `request` fetch geojson from craigslist
request(`https://${city}.craigslist.org/jsonsearch/type/${type}`, (err, res, body) => {
// if the http request fails stop processing and return and call back with an error
if (err) return callback(err)
// translate the raw response from Craigslist into GeoJSON
const geojson = translate(res.body, type)
// add a little bit of metadata to enrich the geojson
// metadata is handled by the output plugin that is responding to the request.
// e.g. https://github.com/koopjs/koop-output-geoservices
geojson.metadata = {
name: `${city} ${type}`,
description: `Craigslist ${type} listings proxied by https://github.com/dmfenton/koop-provider-craigslist`
}
// hand the geojson back to Koop
callback(null, geojson)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment