Skip to content

Instantly share code, notes, and snippets.

@harrietty
Created December 17, 2017 18:06
Show Gist options
  • Select an option

  • Save harrietty/0f01cbb1151641a9ae0ba4669096548d to your computer and use it in GitHub Desktop.

Select an option

Save harrietty/0f01cbb1151641a9ae0ba4669096548d to your computer and use it in GitHub Desktop.
function addResMethods(res) {
res.status = function (code) {
this.statusCode = code;
return this;
};
res.sendStatus = function (code) {
this.statusCode = code;
this.write(getAppropriateTextResponse(code));
this.end();
};
res.set = function (prop, val) {
if (typeof prop === 'object') {
Object.keys(prop).forEach(p => this.setHeader(p, prop[p]));
} else if (prop !== undefined && val !== undefined) {
this.setHeader(prop, val);
}
};
res.send = function (data) {
if (!this.statusCode) this.statusCode = 200;
if (data instanceof Buffer) {
data = data.toString();
if (!this.getHeader('Content-Type')) this.setHeader('Content-Type', 'application/octet-stream');
} else if (typeof data === 'object') {
data = JSON.stringify(data);
if (!this.getHeader('Content-Type')) this.setHeader('Content-Type', 'application/json');
}
this.write(data, 'utf8');
this.end();
};
res.sendFile = function (filePath) {
fs.readFile(filePath, (err, contents) => {
this.setHeader('Content-Type', divineContentType(filePath));
this.send(contents.toString());
});
};
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment