How to use the coap.createServer function in coap

To help you get started, we’ve selected a few coap examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CommonGarden / Grow-IoT / imports / api / coap / coap.js View on Github external
import coap from 'coap';
import url from 'url';
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import Fiber from 'fibers';
import _ from 'underscore';
import { Match } from 'meteor/check';

const server = coap.createServer();

server.on('request', function(req, res) {
  // console.log(req)
  Fiber(function () {
    let urlParts = url.parse(req.url, true);
    let method = urlParts.pathname.replace(/\//g, '');
    let payload = JSON.parse(req.payload.toString());

    console.log(urlParts);
    console.log(method);
    console.log(payload);

    let auth = {
      uuid: payload.uuid,
      token: payload.token
    };
github digsjs / digs / test / unit / lib / digs.spec.js View on Github external
it('should be an DigsEmitter instance', function() {
      expect(new Digs()).to.be.instanceOf(CoAPServer);
    });
github webofthings / wot-book / chapter7-implementation / servers / coap.js View on Github external
var coap = require('coap');

coap.createServer(function(req, res) {
  console.info('Got a CoAP request for %s', req.url);

  if (req.headers['Accept'] != 'application/json') {
    res.code = '4.06';
    return res.end();
  }

  res.setOption('Content-Format', 'application/json');
  res.code = '2.05';
  var result = { co2: 0 };
  result.co2 = random(0, 40);
  res.end(JSON.stringify(result));

}).listen(5683)

function random(low, high) {
github Alliasd / ELIoT / lib / coap-node.js View on Github external
function startListener(cn, callback) {
    var server;

    server = coap.createServer({
        type: cn._config.connectionType,
        proxy: true
    });

    cn.servers[cn.port] = server;

    server.on('request', function (req, rsp) {
        if (!_.isEmpty(req.payload) && req.headers['Content-Format'] === 'application/json') {
            req.payload = JSON.parse(req.payload);
        } else if (!_.isEmpty(req.payload) && req.headers['Content-Format'] === 'application/tlv') {
            req.payload = req.payload.toString('hex');
        } else if (!_.isEmpty(req.payload)) {
            req.payload = req.payload.toString();
        }

        reqHandler(cn, req, rsp);
github PeterEB / coap-shepherd / lib / init.js View on Github external
init._coapServerStart = function (shepherd, callback) {
    var deferred = Q.defer(),
        server = coap.createServer({
            type: shepherd._config.connectionType
        });

    server.on('request', function (req, rsp) {
        if (!_.isEmpty(req.payload)) 
            req.payload = req.payload.toString();

        reqHandler(shepherd, req, rsp);
    });

    server.listen(shepherd._net.port, function (err) {
        if (err)
            deferred.reject(err);
        else
            deferred.resolve(server);
    });
github w3c / web-of-things-framework / transports / coap / server.js View on Github external
var listen = function (settings) {
    
    var server = coap.createServer();

    server.on('request', function (req, res) {
        try {
            if (!req.payload || !req.payload.length) {
                return res.end(JSON.stringify({ "error": COAP_ERROR_INVALID_REQUEST }));
            }
            
            var buffer = req.payload.toString();
            var payload = JSON.parse(buffer);
            var thing = payload.thing;
            var func = payload.func;
            if (!thing || !func) {
                return res.end(JSON.stringify({ "error": COAP_ERROR_INVALID_REQUEST }));
            }
            
            switch (func) {
github mcollina / ponte / lib / coap.js View on Github external
}

  if (typeof opts === "function") {
    cb = opts;
    opts = {};
  }

  var that = this;

  this._persistence = opts.ponte.persistence;
  this._broker = opts.ponte.broker;
  this._ponte = opts.ponte;

  var logger = this._logger = opts.ponte.logger.child({ service: 'CoAP' });

  this.server = coap.createServer(function handler(req, res) {
    var match = req.url.match(rRegexp);
    var topic;

    req.on('error', function(err) {
      logger.info(err);
    });

    res.on('error', function(err) {
      logger.info(err);
    });

    logger.info({ url: req.url, code: req.code, sender: req.rsinfo, headers: req.headers }, 'request received');

    if (match) {
      topic = match[1];
      if (req.method === 'GET') {
github phodal / iot-coap / lib / coap / coap_server.js View on Github external
var coap          = require('coap');
var coapserver    = coap.createServer({});
var URLHandler    = require("../url_handler");

function iotcoap() {
    'use strict';
}

iotcoap.run = function () {
    'use strict';

    var request_handler = require('./coap_request_handler.js');
    coapserver.on('request', function (req, res) {
        if ((URLHandler.url_sanity_check(req.url) === false || URLHandler.url_sanity_check(req.url) === undefined ) && req.url !== "/") {
            request_handler.urlErrorRequest(res);
            return;
        }
github PeterEB / coap-node / lib / coap-node.js View on Github external
function startListener(cn, port, callback) {
    var server;

    server = coap.createServer({
        type: cn._config.connectionType,
        proxy: true
    });

    cn.servers[port] = server;

    server.on('request', function (req, rsp) {        
        if (!_.isEmpty(req.payload) && req.headers['Content-Format'] === 'application/json') {
            req.payload = JSON.parse(req.payload);
        } else if (!_.isEmpty(req.payload) && req.headers['Content-Format'] === 'application/tlv') {
            req.payload = req.payload;
        } else if (!_.isEmpty(req.payload)) {
            req.payload = req.payload.toString();
        }

        reqHandler(cn, req, rsp);
github Alliasd / ELIoT / lib / coap-node.js View on Github external
function startMultiListener(cn, callback) {
    var server_multi;

    server_multi = coap.createServer({
        type: cn._config.connectionType,
        //proxy: true,
        multicastAddress: '224.0.1.187'
    });

    cn.servers['5683'] = server_multi;

    server_multi.on('request', function (req, rsp) {
        //if ((req.rsinfo['address'] === '127.0.0.1') || (req.rsinfo['address'] != cn.ip)) {
        if (!_.isEmpty(req.payload)) {
            req.payload = JSON.parse(req.payload);
        } else if (!_.isEmpty(req.payload)) {
            req.payload = req.payload.toString();
        }
        reqHandler(cn, req, rsp);
      //} else {