0 PrefaceThis article describes if you install and use Node-coap. 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 Agreement Learning--coap Foundation" "Coap Learning Notes--coap resource Discovery" "COAP Learning Notes-Process request flow when server-side busy" "Raspberry Pi Learning Notes--webiopi installation and Getting Started" Webiopi integrates Coap Server, The Raspberry Pi Gpio can be controlled conveniently through the COAP protocol.
1 Nodejs Installation"1" Download Nodejs source code Package
"2" Linux installation trilogy, enter the following command in turn:
./configure
Make
sudo make install
"3" necessary checks
node--version
2 Installing and using Node-coap"1" Create a new working directory
mkdir Hello-coap
"2" Enter the directory to install Node-coap
npm Install Node-coap--saveNote that there is a slight difference in installing Node-coap in Windows, see--coap Learning Notes--nodejs Node-coap Installation and use (Windows platform)
3 Simple Examples
3.1 Server Code
Const COAP = require (' Coap ') , server = Coap.createserver () server.on (' Request ', function (req, res) { Res.end (' Hello ' + req.url.split ('/') [1] + ' \ n ')}) Server.listen (function () { console.log (' Server Started ')})
"description"
The engineering of the server is also very simple, res.end (' Hello ' + req.url.split ('/') [1] + ' \ n ') from the sentence program can be seen if the request URL is <server ip>/<name> Then the server returns Hello <Name>
"Running in the background"
node Server.js &
"If you need to shut down the server process"
# view Server.js's PID
PS aux | grep server.js
# Kill Process
Kill <pid>
3.2 Client Testing
Const COAP = require (' Coap ') , req = coap.request (' coap://localhost/xukai871105 ') req.on (' response ', Function (res) { res.pipe (process.stdout)}) Req.end ()
"description"
The requested URL is coap://localhost/xukai871105, note that the URL starts with Coap instead of HTTP
"Run"
node Client.js
"Back"
Hello xukai871105
3.3 Browser TestingBrowser testing is more intuitive and requires the Coap plugin to be installed in the Firefox browser.
"1" Enter coap://localhost/xukai871105 in the browser address bar
"2" Click Get in the toolbar
"3" Payload output Hello xukai871105 in one column
Figure 1 Browser Testing
3.4 Command Test
In addition to using browser and client tool testing, you can also coap command-line tests, similar to Nodejs-based command-line tools and curl tools. You need to install COAP-CLI correctly before using the COAP command-line tool.
Installation
(sudo) npm install coap-cli-g
Test
Coap Get coap://localhost/xukai871105
Returns
(2.05) Hello xukai871105
(2.05) can be understood as the HTTP in the OK. For more instructions on Coap Please read the reference "2"
4 References
"1" Node-coap code Warehouse
"2" coap-cli coap command line tool
Coap Learning notes--nodejs Node-coap installation and use (Linux platform)