Skip to content

Commit

Permalink
mv the coap to server path
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Aug 22, 2014
1 parent 9aa886f commit 40b249f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 46 deletions.
44 changes: 2 additions & 42 deletions index.js
@@ -1,46 +1,6 @@
const coap = require('coap')
,coapserver = coap.createServer({})
,fs = require('fs')
,file = './iot.json'
,rest = require('./server/rest');

function iotcoap(){

}

iotcoap.run = function(){
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
var config = JSON.parse(data);
module.exports.config = config;

startIOT();
});

function startIOT(){
const request_handler = require('./server/request_handler.js');
coapserver.on('request', function(req, res) {
switch(req.method){
case "GET": request_handler.getHandler(req, res);
break;
case "POST":
case "PUT": request_handler.syncHandler(req, res);
break;
case "DELETE": request_handler.deleteHandler(req, res);
break;
default: request_handler.errorRequest(res);
break;
}
});

coapserver.listen(function() {
console.log('coap listening at coap://0.0.0.0:5683');
});
}
};
,rest = require('./server/rest')
,iotcoap = require('./server/coap');

module.exports = iotcoap;
module.exports.rest = rest;
2 changes: 1 addition & 1 deletion method_test/put.js
Expand Up @@ -11,7 +11,7 @@ var req = request({
req.setHeader("Accept", "application/json");
var bl = require('bl');

req.setOption('Block2', [new Buffer('3'),new Buffer('phodal'), new Buffer('13'), new Buffer('12')]);
req.setOption('Block2', [new Buffer('3'),new Buffer("'phodal'"), new Buffer('13'), new Buffer('12')]);
req.on('response', function(res) {
res.pipe(bl(function(err, data) {
// var json = JSON.parse(data);
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "iot-coap",
"version": "0.1.3",
"version": "0.1.8",
"description": "Mini IoT CoAP Version",
"repository": {
"type": "git",
Expand Down
44 changes: 44 additions & 0 deletions server/coap.js
@@ -0,0 +1,44 @@
const coap = require('coap')
,coapserver = coap.createServer({})
,fs = require('fs')
,file = './iot.json';

function iotcoap(){

}

iotcoap.run = function(){
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
var config = JSON.parse(data);
module.exports.config = config;

startIOT();
});

function startIOT(){
const request_handler = require('./request_handler.js');
coapserver.on('request', function(req, res) {
switch(req.method){
case "GET": request_handler.getHandler(req, res);
break;
case "POST":
case "PUT": request_handler.syncHandler(req, res);
break;
case "DELETE": request_handler.deleteHandler(req, res);
break;
default: request_handler.errorRequest(res);
break;
}
});

coapserver.listen(function() {
console.log('coap listening at coap://0.0.0.0:5683');
});
}
};

module.exports = iotcoap;
1 change: 1 addition & 0 deletions server/rest.js
Expand Up @@ -18,6 +18,7 @@ rest.run = function(){
module.exports.config = config;
startRESTIOT(config);
});

function startRESTIOT(config){
const rest_helper = require("./rest_helper.js");

Expand Down
5 changes: 3 additions & 2 deletions server/rest_helper.js
@@ -1,5 +1,5 @@
const restdb_helper = require('./db_helper.js')
,_ = require("underscore");
,_ = require("underscore");

function rest_helper(){

Expand All @@ -12,6 +12,7 @@ rest_helper.respond = function(req, res, next) {
})
};


rest_helper.get_respond = function (req, res, next) {
restdb_helper.urlQueryData(req.url, function(e){
res.send(JSON.parse(e));
Expand All @@ -26,7 +27,7 @@ rest_helper.post_respond = function (req, res, next) {
key = "'" + key + "'";
}
data.push(key);
})
});
restdb_helper.syncData(data, function(e){
res.send({});
next();
Expand Down

0 comments on commit 40b249f

Please sign in to comment.