Node.js REST application With JSON Data and file

60 views
Skip to first unread message

vivek gs

unread,
Apr 9, 2015, 11:28:23 AM4/9/15
to nod...@googlegroups.com

Hi,
I have a node.js REST application which will handle the JSON Data and a log file from the client(mobile). I am using the busboy module to handle both json and file in a request. Here I am facing the issue of sending the response to client. Has the field/json data will finish the work and it can't wait until the file will process the request.

Is their any good way to handle both and wait for each one to complete and send response after all the logic is complete.  


Thanks

Rob Steward

unread,
Apr 10, 2015, 9:25:53 AM4/10/15
to nod...@googlegroups.com
Without a code example it's hard to tell exactly what you want, but it sounds like promises might help you here. I would recommend these libraries, and a useful guide can be found here.

A contrived example:

function handleJSON(body) {
  // ...
  // return a promise
}

function writeLogFile(request) {
  // ...
  // return a promise
}

function httpHandler(request, response) {
  Promise.all([
    handleJSON(request.body),
    writeLogFile(request)
  ]).then(function(postResult, fileResult) {
    // postResult and fileResult will the results of handleJSON and writeLogFile respectively

    // ...

    // response.end('Done!');
  });
Reply all
Reply to author
Forward
0 new messages