【问题标题】:COAP API trigger using node js使用节点 js 的 COAP API 触发器
【发布时间】:2016-08-31 05:58:20
【问题描述】:

我将 COAP API 开发成 java。它运作良好。 示例 URL: coap://localhost:5683/test-url 。此 api 将使用电子设备触发。

我在 nodejs 中开发了另一个项目。但是我想通过node js触发COAP api。

我关注了这个网址。 node-coap 但它不工作。请任何人推荐我。

const coap    = require('../') // or coap
    , server  = coap.createServer()

server.on('request', function(req, res) {
  res.end('Hello ' + req.url.split('/')[1] + '\n')
})

// the default CoAP port is 5683 
server.listen(function() {

  var req = coap.request('coap://localhost:5683/test-url');
  req.on('response', function(res) {
    res.pipe(process.stdout)
    res.on('end', function() {
      process.exit(0)
    })
  })

  req.end()
});

错误详情:

Error: bind EADDRINUSE 0.0.0.0:5683
    at Object.exports._errnoException (util.js:893:11)
    at exports._exceptionWithHostPort (util.js:916:20)
    at dgram.js:221:18

【问题讨论】:

    标签: javascript node.js api http coap


    【解决方案1】:

    错误消息 EADDRINUSE 表明端口 (5683) 已在使用中。 您可以更改端口或终止在 5683 上运行的进程并尝试重新启动您的应用程序。

    您可以像下面这样更改端口:

    var PORT = 3000;
    server.listen(PORT, function() {
    
      var req = coap.request('coap://localhost:5683/test-url');
      req.on('response', function(res) {
        res.pipe(process.stdout)
        res.on('end', function() {
          process.exit(0)
        })
      })
    
      req.end()
    });
    

    【讨论】:

    • 是的。我试图在 lcoal 节点项目中更改开发 url,如 - coap://api-test.com:5683/test-url 。但同样的错误
    • 不是这样的。您应该更改 node-coap 应用程序的端口。我已将代码 sn-p 添加到答案中
    • @pakTech - 是的,问题解决了。我在答案部分附上了我的代码。请检查一下。
    【解决方案2】:

    以下代码对我有用。

    var coap = require('coap');    
    var coapConnection = {
        host : url,
        pathname : '/path',
        method : 'POST',
        port : '5683',
        confirmable : true
    };
    
    var req = coap.request(coapConnection);
    req.write(JSON.stringify(spec.body));
    req.on('response', function(response) {
        var d = response.payload;
        spec.resp.response = JSON.parse(d.toString('utf8'));
        response.pipe(process.stdout);
        response.on('end', function() {
            console.log("Success");
        });
    });
    req.end(); 
    

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 2021-01-18
      • 1970-01-01
      • 2023-02-19
      • 1970-01-01
      相关资源
      最近更新 更多