Skip to content

Commit

Permalink
Almost there
Browse files Browse the repository at this point in the history
  • Loading branch information
pxai committed May 5, 2018
1 parent 474f62c commit e6ed70f
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 1 deletion.
10 changes: 10 additions & 0 deletions miniapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Miniapi {
this.hostname = 'localhost';
this.id = 'id';
this.persist = false;
this.file = null;
}

withName (name) {
Expand Down Expand Up @@ -56,6 +57,7 @@ class Miniapi {
withDataFrom (file) {
try {
this.data = JSON.parse(fs.readFileSync(file));
this.file = file;
} catch (e) {
console.log('ERROR' + e);
//this.data = [];
Expand Down Expand Up @@ -144,8 +146,16 @@ class Miniapi {
break;
}

if (method !== 'GET') { this.persistIfEnabled(); }
res.end(`${JSON.stringify(res.data)}\n`);
}

persistIfEnabled() {
if (this.getPersist()) {
console.log('PErsisting to: ', this.persist, this.file);
fs.writeFileSync(this.file, JSON.stringify(this.data));
}
}
};

module.exports=new Miniapi('user');
2 changes: 1 addition & 1 deletion sample.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[ { "_id": 1, "name": "John" }, { "_id": 2, "name": "Ada" } ]
[object Object],[object Object],[object Object],[object Object],[object Object]
4 changes: 4 additions & 0 deletions test/miniapi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const DATA = [ { id: 1, name: 'Bob' }, { id: 2, name: 'Alice' } ];


describe('basic tests working', () => {
afterEach(() => {
miniapi.stop;
miniapi.persist = false;
});
it('should not be null', () => {
expect(miniapi).toBeTruthy();
});
Expand Down
160 changes: 160 additions & 0 deletions ç
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
const http = require('http');
const requestHandler = require('./requestHandler');
const log = require('./log');
const fs = require('fs');

let data = [{ id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];


class Miniapi {
constructor (name) {
this.name = name;
this.port = 3000;
this.contentType = 'application/json';
this.data = data;
this.hostname = 'localhost';
this.id = 'id';
this.persist = false;
this.file = null;
}

withName (name) {
this.name = name;
return this;
}

getName () {
return this.name;
}

withPort (port) {
this.port = port;
return this;
}

getPort () {
return this.port;
}

withContentType (contentType) {
this.contentType = contentType;
return this;
}

getContentType () {
return this.contentType;
}

withData (data) {
this.data = data;
return this;
}

getData () {
return this.data;
}

withDataFrom (file) {
try {
this.data = JSON.parse(fs.readFileSync(file));
this.file = file;
} catch (e) {
console.log('ERROR' + e);
//this.data = [];
}
return this;
}

withId (id) {
this.id = id;
return this;
}

getId() {
return this.id;
}

getPersist() {
return this.persist;
}

withPersist() {
this.persist = true;
return this;
}

start () {
requestHandler.setData(this.data);
requestHandler.setName(this.name);
requestHandler.setId(this.id);

this.server = http.createServer((req, res) => {
let url = req.url.split("/");
res.setHeader('Content-Type', this.contentType);
if (req.method === 'POST' || req.method === 'PUT' || req.method === 'PATCH') {
var body = '';
var self = this;
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
self.reply(url, req.method, res, body);
});
} else {
this.reply(url, req.method, res);
}
});

this.server.listen(this.port, this.hostname, () => {
log.info(`[miniapi server running at http://${this.hostname}:${this.port}/]`);
});
}

stop () {
this.data = data;
console.log('Stopped: , data: ' , this.data);
this.server.close();
}

version () {
return '1.0';
}

reply (url, method, res, requestBody) {
res.data = {};
res.statusCode = 404;

switch (method) {
case 'GET':
res = requestHandler.get(url, method, res);
break;
case 'POST':
res = requestHandler.post(url, method, res, requestBody);
break;
case 'PUT':
res = requestHandler.put(url, method, res, requestBody);
break;
case 'PATCH':
res = requestHandler.patch(url, method, res, requestBody);
break;
case 'DELETE':
res = requestHandler.delete(url, method, res);
break;
default:
res.data = { error: 'Whatever you tried, it is not supported.'};
res.statusCode = 404;
break;
}

if (method !== 'GET') { this.persistIfEnabled(); }
res.end(`${JSON.stringify(res.data)}\n`);
}

persistIfEnabled() {
if (this.withPersist) {
fs.writeFileSync(this.file, this.data);
}
}
};

module.exports=new Miniapi('user');

0 comments on commit e6ed70f

Please sign in to comment.