Skip to content

Instantly share code, notes, and snippets.

@Janpot
Created February 24, 2014 17:29
Show Gist options
  • Save Janpot/9192792 to your computer and use it in GitHub Desktop.
Save Janpot/9192792 to your computer and use it in GitHub Desktop.
var reportRoute = function (req, res) {
// Validate input parameters
req.assert("userId").notEmpty().isInt();
req.assert("url").notEmpty().isUrl();
req.assert("reportId").notEmpty().isInt();
// Validate input
var errors = req.validationErrors();
// Handle errors
if (errors) {
res.json({ error: "Invalid parameter(s)", details: errors });
} else {
// Map a few variables
var userId = parseInt(req.param("userId"), 10)
, url = req.param("url")
, reportId = req.param("reportId");
reportStore.getReport(reportId, !!userId)
.then(function (report) {
return report.fetchCriteria()
})
.then(function (report) {
if (report.isFallbackReport) {
// store the fallback
report.storeCriteria();
}
// serialize report and send to client
report.createReadStream()
.pipe(new Mustachifier(/* ... */))
})
.fail(function () {
// send bad response
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment