Skip to content

Commit

Permalink
feat: create onRequestEnd event handler, request body parser controll…
Browse files Browse the repository at this point in the history
…er, and on response finish hous

The onRequestEnd method reacts and handles the request-response logic, the other controls request
body parsing, then the last performs house keeping once the response has been sent to the kernel
  • Loading branch information
teclone committed Jul 23, 2018
1 parent 380216e commit d59f83f
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/modules/RServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,69 @@ export default class {
return false;
}

/**
* parse all request data
*/
parseRequestData(request, url, buffers) {
//parse query
request.query = this.bodyParser.parseQueryString(url);

//parse the request body
if (buffers.length > 0) {
let result = this.bodyParser.parse(Buffer.concat(buffers),
request.headers['content-type']);

request.body = result.body;
request.files = result.files;
}

//combine the body and query into a data property
request.data = Object.assign({}, request.query, request.body);
}

/**
* perform house keeping
*@param {http.IncomingMessage} request - the request object
*/
onResponseFinish(request) {
this.bodyParser.cleanUpTempFiles(request.files);
}

/**
* handle request data event
*@param {http.IncomingMessage} request - the request object
*@param {RServerResponse} response - the response object
*@param {Object} bufferDetails - the buffer details
*@param {number} bufferDetails.size - the buffer size
*@param {Array} bufferDetails.buffers - array containing chunks of buffer data
*/
onRequestEnd(request, response, bufferDetails) {
let {url, method, headers} = request;

request.files = {};
request.query = {};
request.body = {};

//clean up resources once the response has been sent out
response.on('finish', Util.generateCallback(this.onResponseFinish, this,
[request]
));

if (this.staticfileServer.serve(url, method, headers, response))
return;

this.parseRequestData(request, url, bufferDetails.buffers);

if (this.runRoutes(url, method, request, response))
return;

//send 404 response if router did not resolved
let httpErrors = this.config.httpErrors;
this.staticfileServer.serveHttpErrorFile(
response, 404, httpErrors.baseDir, httpErrors['404']
);
}

/**
* handle request data event
*/
Expand Down

0 comments on commit d59f83f

Please sign in to comment.