var coap = require('coap');
// POST请求
var req = coap.request({
observe: false,
pathname: '/Message',
method: 'POST',
options: {
'Accept': 'application/json'
}
});
req.setOption("555", Buffer.from([1, 2, 3]));
// 监听响应
req.on('response', function(res){
console.log('响应码:', res.code);
if(res.code !== '2.05') {
return process.exit(1);
}
// 将响应结果输出
res.pipe(process.stdout);
});
// 结束请求
req.end();
// GET请求
var coap = require('coap');
var req = coap.request({
pathname: '/Message',
options: {
'Accept': 'application/json'
}
});
// 监听响应
req.on('response', function(res){
console.log('响应码:', res.code);
if(res.code !== '2.05') {
return process.exit(1);
}
// 将响应结果输出
res.pipe(process.stdout);
});
// 结束请求
req.end();