Coap Learning Note--nodejs NODE-COAP return JSON packet

Source: Internet
Author: User
Tags response code

0 PrefaceThis article explains how to use NODE-COAP to return JSON packets. COAP is a network-oriented application layer protocol stack developed specifically for IoT systems, and COAP is built on the UDP protocol to minimize network overhead and features of HTTP restful types. The NODE-COAP uses Nodejs to implement the COAP client and server side."test Environment"--ubuntu/linux"Related blog posts"
"CoAP Protocol Documentation--the Constrained application Protocol (CoAP)""Coap Agreement Learning--coap Foundation" "Coap Learning Notes--coap resource Discovery" "COAP Learning Notes-Process request flow when server-side busy" Coap Learning notes--nodejs Node-coap installation and use (Linux platform) "Coap Learning notes--nodejs Node-coap Installation and use (Windows platform)" "Raspberry Pi Learning Notes--webiopi installation and Getting Started" Webiopi integrates Coap Server, which allows you to conveniently control the Raspberry Pi Gpio via the COAP protocol.
1 server Code
var coap = require (' Coap '); var server = Coap.createserver (); Server.on (' Request ', function (req, res) {    Console.log ( Req.headers);    The request header must include Application/json    if (req.headers[' Accept ']! = ' Application/json ') {        res.code = ' 4.06 ';        return Res.end ();    }    Res.setoption (' Content-format ', ' Application/json ');    Res.end (json.stringify ({        Hello: "World"    })); Server.listen (function () {    console.log (' server started ');});
"Simple description" "1" req.headers[' Accept ']! = ' Application/json ' due to the return of the JSON packet, it is advisable to include Accept:application/json "2" in the request header res.setoption ( ' Content-format ', ' Application/json '); Set the response header "3" Res.end (json.stringify ({hello: "World"}); Returns a JSON packet with a fixed content {"Hello": "World"}
2 Client Code
var coap = require (' Coap '); var bl = require (' bl '); var req = coap.request ({    pathname: '/',    options: {        ' Accept ') : ' Application/json '}    ); Req.on (' Response ', function (res) {    console.log (' Response code ', Res.code);    if (Res.code!== ' 2.05 ') return Process.exit (1);    Res.pipe (BL (err, data) {        var json = json.parse (data);        Console.log (JSON);        Process.exit (0);    })); Req.end ();
"Execution Return"Response Code 2.05
{Hello: ' World '}"Brief description" "1" var req = coap.request ({pathname: '/', options: {' Accept ': ' Application/json '}}); The request header includes Accept:application/json. 3 Browser TestingIf Accept:application/json is not included in the request header, the server returns a 4.06 error equivalent to 404 in HTTP.
Figure 1 return error results start debug mode, set the Accept option to Application/json.
Figure 2 returning the correct result
4 Wireshark Grab Bag4.1 Using the Tcpdump toolsudo tcpdump-i lo-w ~/temp/coap-json.cap"Simple description" "1"-I parameter make Nic, lo represents loopback
"2"-W set the output file path and file name, saved here in the CAP format file

4.2 Cap file Import in Wireshark
Figure 3 Wireshark Grab Bag5 SummaryFeeling that the COAP protocol is not enough in depth, the protocol itself still needs to spend some time to digest.

Coap Learning Note--nodejs NODE-COAP return JSON packet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.