From 52bbd6c4a0bf91893ffb28bf735d34b939504e6b Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Fri, 22 Mar 2013 19:33:32 +0300 Subject: [PATCH] Fixed invalid json in API query results. Added total count in response header. --- api.coffee | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/api.coffee b/api.coffee index 99c5b56..bc8dc13 100644 --- a/api.coffee +++ b/api.coffee @@ -323,17 +323,19 @@ else cur.skip(parseInt(urlParts.query.skip)) if urlParts.query.skip? cur.limit(parseInt(urlParts.query.limit)) if urlParts.query.limit? - - response.writeHead(200, {'Content-Type' : 'application/json'}) - response.write("[\n") - cur.each((err, item) -> - if item is null - response.end(']') - else - addAliases(item) if collectionName is 'devices' - response.write(JSON.stringify(item) + ",\n") + cur.count((err, total) -> + response.writeHead(200, {'Content-Type' : 'application/json', 'total' : total}) + response.write("[\n") + i = 0 + cur.each((err, item) -> + if item is null + response.end("\n]") + else + response.write(",\n") if i++ + addAliases(item) if collectionName is 'devices' + response.write(JSON.stringify(item)) + ) ) - return else response.writeHead 404 response.end('404 Not Found')