Skip to content

Commit

Permalink
Update the client & server to Return a JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Aug 14, 2014
1 parent a42c31c commit ed09295
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
12 changes: 8 additions & 4 deletions client.js
@@ -1,13 +1,17 @@
var coap = require('coap');
var coap = require('coap');
var requestURI = 'coap://localhost/';
var name = 'World';
var url = require('url').parse(requestURI + name);
var req = coap.request(url);

coap.registerFormat('application/json', 50);
var bl = require('bl');

req.on('response', function(res) {
res.pipe(process.stdout);
res.pipe(bl(function(err, data) {
var json = JSON.parse(data);
console.log(json);
process.exit(0);
}));

});

req.end();
14 changes: 6 additions & 8 deletions server/server.js
Expand Up @@ -28,17 +28,15 @@ function start_server() {

server.on('request', function(req, res) {
if (req.headers['GET'] !== 0) {
res.end('GET ' + req.url.split('/')[1] + '\n');
res.setOption('Content-Format', 'application/json');

res.end(JSON.stringify({
hello: req.url.split('/')[1]
}));
res.code = '4.06';
} else {
res.end('Hello ' + req.url.split('/')[1] + '\n');
};

res.setOption('Content-Format', 'application/json');

res.end(JSON.stringify({
hello: "world"
}));
}
});

server.listen(function() {
Expand Down

0 comments on commit ed09295

Please sign in to comment.