Skip to content

Instantly share code, notes, and snippets.

@jaawerth
Forked from anonymous/mygist.js
Last active September 21, 2016 16:29
Show Gist options
  • Save jaawerth/bf48b51c5226adaf910000609b73aefb to your computer and use it in GitHub Desktop.
Save jaawerth/bf48b51c5226adaf910000609b73aefb to your computer and use it in GitHub Desktop.
'use strict';
const fetch = require('./fetch');
module.exports = function fetchMW(req, res, next) {
fetch(req.query.shape_id, (err, results) => {
if (err) {
return next(err);
} else {
req.dbResults = results;
return next();
}
})
};
'use strict';
module.exports = fetch;
// use a prepared statement if using user input in the query to avoid SQL injection attacks!
const stmt = db.prepare('SELECT shape_pt_lat, shape_pt_lon FROM shapes where shape_id=?');
function fetch (shapeId, callback){
const data = [];
// this actually executes the query, and passes in the two callbacks defined below
stmt.each([shapeId], addRow, onComplete);
function onComplete(err, numRows) {
if (err) {
return callback(err);
} else {
return callback(null, data);
}
}
function addRow(e, rows) => {
if (e) {
data.push(e); // probably want to do something other than just pushing the error onto the same array as the results
} else {
data.push({
x: rows['shape_pt_lat'],
y: rows['shape_pt_lon']
});
}
}
}
'use strict';
const fetch = require('./fetch');
app.get('/data', (request, res) => {
res.setHeader("Access-Control-Allow-Origin",'http://localhost:8080');
fetch(request, function(err, data){
console.log(data, "dickt")
if (err) {
// do something about error - pass to err middleware with next(err) or send some kind of response
} else {
res.json(data);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment