Skip to content

Commit

Permalink
1.2.5
Browse files Browse the repository at this point in the history
- added json method to Response prototype
  • Loading branch information
tones31 committed Sep 15, 2019
1 parent 391ff5f commit 4c2f2cc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 deletions.
70 changes: 36 additions & 34 deletions http/httpServer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
const ServerResponse = require('http').ServerResponse;
const Querystring = require('query-string');
const Server = require('../server/server');
const Fs = require('fs');
const Path = require('path');
const Mime = require('mime-types');
const Router = require('find-my-way');

if(typeof ServerResponse.prototype.json === "undefined") {

/**
* Add the JSON method to a response object
* which will stringify an object as the
* data response, set the content type to
* application/json, and set the status code to 200.
* If the JSON cannot be stringified, the
* status code will be set to 500.
* This ends the respnose.
* @param {object} response
* @return {Response}
*/
ServerResponse.prototype.json = function(data){
let json = null;
try {
json = JSON.stringify(data);
}
catch (e){
console.error(e);
}
if(json){
this.statusCode = 200;
this.setHeader('Content-Type', 'application/json');
this.write(json);
this.end();
}
else {
this.statusCode = 500;
this.end();
}
return this;
};
}

/**
* HTTP Server.
* A barebones HTTP Server ready to be used.
Expand Down Expand Up @@ -375,43 +411,9 @@ class HttpServer extends Server {
* @return {HttpServer}
*/
routeRequest(request, response){
this.addJsonMethod(response);
this.router.lookup(request, response);
return this;
}

/**
* Add the JSON method to a response object
* which will stringify an object as the
* data response, set the content type to
* application/json, and set the status code to 200.
* If the JSON cannot be stringified, the
* status code will be set to 500.
* This ends the respnose.
* @param {Response} response
*/
addJsonMethod(response){
response.json = function(data){
let json = null;
try {
json = JSON.stringify(data);
}
catch (e){
console.error(e);
}
if(json){
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
response.write(json);
response.end();
}
else {
response.statusCode = 500;
response.end();
}
};
return response;
}
}

module.exports = HttpServer;
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@voliware/node-server",
"version": "1.2.4",
"version": "1.2.5",
"author": "Anthony Agostino",
"description": "Node Server",
"keywords": [
Expand All @@ -13,11 +13,11 @@
],
"main": "index.js",
"dependencies": {
"@voliware/logger": "^1.4.1",
"find-my-way": "^2.1.0",
"@voliware/logger": "^1.4.2",
"find-my-way": "^2.1.1",
"microtime": "^2.1.8",
"mime-types": "^2.1.24",
"query-string": "^6.8.2",
"query-string": "^6.8.3",
"ws": "^4.0.0"
},
"devDependencies": {},
Expand Down

0 comments on commit 4c2f2cc

Please sign in to comment.