-
-
Save jaawerth/bf48b51c5226adaf910000609b73aefb to your computer and use it in GitHub Desktop.
This file contains 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
'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(); | |
} | |
}) | |
}; |
This file contains 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
'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'] | |
}); | |
} | |
} | |
} |
This file contains 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
'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