@sap/xsodata
Version:
Expose data from a HANA database as OData V2 service with help of .xsodata files.
64 lines (45 loc) • 1.41 kB
JavaScript
;
var _ = require('lodash');
var http = require('http');
module.exports.createResponse = function () {
return new SimpleResponse();
};
function SimpleResponse() {
this.statusCode = null;
this.headers = {};
this.data = '';
}
SimpleResponse.prototype.writeHead = function (code, headers) {
this.statusCode = code;
_.assign(this.headers, headers);
};
SimpleResponse.prototype.setHeader = function (name, value) {
this.headers[name] = value;
};
SimpleResponse.prototype.status = function (code) {
this.statusCode = code;
return this;
};
SimpleResponse.prototype.write = function (data) {
this.data += data;
return this;
};
SimpleResponse.prototype.end = function () {
};
SimpleResponse.prototype.writeToBatchResponse = function (response,contentId) {
var value;
var headers = this.headers;
response.write('HTTP/1.1 ' + this.statusCode + ' ' + http.STATUS_CODES['' + this.statusCode] + '\r\n');
Object.keys(headers).forEach(function (key) {
value = headers[key];
response.write(key + ': ' + value + '\r\n');
});
if (contentId) {
response.write('Content-ID: ' + contentId.id+ '\r\n');
}
response.write('\r\n'); // end of header
response.write(this.data);
if (this.data.length > 0 ) {
response.write('\r\n'); //as used in OData_Specification_V2.0 batch samples
}
};