Add CoAP-over-UDP hello server in Rust

A minimal CoAP (RFC 7252) server built on coap-lite for message encoding
and a tokio UdpSocket for transport. The socket, read loop and routing are
kept in src/main.rs rather than behind a framework.

Routes:
  GET  /hello[?name=X]  -> 2.05 "Hello, X!" (defaults to World)
  POST /hello           -> 2.01, name from a raw text or JSON body
  GET  /health          -> 2.05 status + uptime

Unknown paths return 4.04, known paths with the wrong method 4.05.

Includes a CLI client (src/bin/client.rs) for smoke tests and unit tests
covering the greeting, payload parsing and query decoding. Verified against
node-coap as an independent implementation to confirm wire compatibility.

The earlier Node.js prototype is kept under nodejs/ for reference.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-03 10:28:24 +06:00
commit 137b9d9547
10 changed files with 1167 additions and 0 deletions

32
nodejs/client.js Normal file
View File

@@ -0,0 +1,32 @@
'use strict';
// Tiny CLI client:
// node client.js get /hello?name=Imran
// node client.js post /hello Imran
// node client.js get /health
const coap = require('coap');
const [, , methodArg = 'get', pathArg = '/hello', payload] = process.argv;
const req = coap.request({
host: process.env.COAP_HOST || 'localhost',
port: Number(process.env.COAP_PORT) || 5683,
pathname: pathArg.split('?')[0],
query: pathArg.split('?')[1] || '',
method: methodArg.toUpperCase(),
confirmable: true,
});
req.on('response', (res) => {
console.log(`<- ${res.code} ${res.payload.toString()}`);
process.exit(0);
});
req.on('error', (err) => {
console.error('request failed:', err.message);
process.exit(1);
});
if (payload) req.write(payload);
req.end();

309
nodejs/package-lock.json generated Normal file
View File

@@ -0,0 +1,309 @@
{
"name": "coapwithudp",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "coapwithudp",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"coap": "^1.6.0"
}
},
"node_modules/@types/node": {
"version": "26.4.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-26.4.1.tgz",
"integrity": "sha512-k97ENvZWtvA6yqz5/FS6a7duDgOPEeOQOc2iKS/nY6mX6qJUKtLnWzQS+Xj6tXweyj6ZcTAK2Qecetnvi9nCLA==",
"license": "MIT",
"dependencies": {
"undici-types": "~8.3.0"
}
},
"node_modules/@types/readable-stream": {
"version": "4.0.24",
"resolved": "https://registry.npmjs.org/@types/readable-stream/-/readable-stream-4.0.24.tgz",
"integrity": "sha512-NRvUNC/JFGPJvqdAfEve8oginbM6V08u5NzLWpG8MwA2kTPOLnqk+wpwuPT+mp3aUsxyuT6m2gnrPuHYCruzEg==",
"license": "MIT",
"dependencies": {
"@types/node": "*"
}
},
"node_modules/abort-controller": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz",
"integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==",
"license": "MIT",
"dependencies": {
"event-target-shim": "^5.0.0"
},
"engines": {
"node": ">=6.5"
}
},
"node_modules/base64-js": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/bl": {
"version": "6.1.6",
"resolved": "https://registry.npmjs.org/bl/-/bl-6.1.6.tgz",
"integrity": "sha512-jLsPgN/YSvPUg9UX0Kd73CXpm2Psg9FxMeCSXnk3WBO3CMT10JMwijubhGfHCnFu6TPn1ei3b975dxv7K2pWVg==",
"license": "MIT",
"dependencies": {
"@types/readable-stream": "^4.0.0",
"buffer": "^6.0.3",
"inherits": "^2.0.4",
"readable-stream": "^4.2.0"
}
},
"node_modules/buffer": {
"version": "6.0.3",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz",
"integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT",
"dependencies": {
"base64-js": "^1.3.1",
"ieee754": "^1.2.1"
}
},
"node_modules/capitalize": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/capitalize/-/capitalize-2.0.4.tgz",
"integrity": "sha512-wcSyiFqXRYyCoqu0o0ekXzJAKCLMkqWS5QWGlgTJFJKwRmI6pzcN2hBl5VPq9RzLW5Uf4FF/V/lcFfjCtVak2w==",
"license": "MIT"
},
"node_modules/cbor": {
"version": "9.0.2",
"resolved": "https://registry.npmjs.org/cbor/-/cbor-9.0.2.tgz",
"integrity": "sha512-JPypkxsB10s9QOWwa6zwPzqE1Md3vqpPc+cai4sAecuCsRyAtAl/pMyhPlMbT/xtPnm2dznJZYRLui57qiRhaQ==",
"license": "MIT",
"dependencies": {
"nofilter": "^3.1.0"
},
"engines": {
"node": ">=16"
}
},
"node_modules/coap": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/coap/-/coap-1.6.0.tgz",
"integrity": "sha512-AvPHT2yQet0wOhlmIqFk11NQPnsKt30yRk6IGp9Yhb6Xnp3ozExPfklJXiE28Xb47tjU0M2OV2W++h8f350Mjg==",
"license": "MIT",
"dependencies": {
"@types/readable-stream": "^4.0.14",
"bl": "^6.0.12",
"capitalize": "^2.0.4",
"coap-oscore": "^2.2.3",
"coap-packet": "^1.1.1",
"debug": "^4.3.5",
"fastseries": "^2.0.0",
"lru-cache": "^11.0.2",
"readable-stream": "^4.5.2"
},
"engines": {
"node": ">=18"
}
},
"node_modules/coap-oscore": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/coap-oscore/-/coap-oscore-2.2.3.tgz",
"integrity": "sha512-uF592/QIDgxbwhvAHCiWJat0xIoWsc8K7ubzYK1jXM4ihe50sHLAM7pl361NzZXLOMkkYnHCBSq07DA56OIs/A==",
"license": "MIT",
"dependencies": {
"cbor": "^9.0.2"
}
},
"node_modules/coap-packet": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/coap-packet/-/coap-packet-1.1.1.tgz",
"integrity": "sha512-Bkz2ZKI/7hU2gm6nUuo5l+MBSkdFJx7My1ZgNEhKUC7K2yYfQYVbBPRa64BBYLcEcYgaSlau4A1Uw5xfM2I0zw==",
"license": "MIT",
"engines": {
"node": ">= 0.10"
}
},
"node_modules/debug": {
"version": "4.4.3",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",
"integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==",
"license": "MIT",
"dependencies": {
"ms": "^2.1.3"
},
"engines": {
"node": ">=6.0"
},
"peerDependenciesMeta": {
"supports-color": {
"optional": true
}
}
},
"node_modules/event-target-shim": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz",
"integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==",
"license": "MIT",
"engines": {
"node": ">=6"
}
},
"node_modules/events": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz",
"integrity": "sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==",
"license": "MIT",
"engines": {
"node": ">=0.8.x"
}
},
"node_modules/fastseries": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/fastseries/-/fastseries-2.0.0.tgz",
"integrity": "sha512-XBU9RXeoYc2/VnvMhplAxEmZLfIk7cvTBu+xwoBuTI8pL19E03cmca17QQycKIdxgwCeFA/a4u27gv1h3ya5LQ==",
"license": "ISC"
},
"node_modules/ieee754": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
"integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "BSD-3-Clause"
},
"node_modules/inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
"license": "ISC"
},
"node_modules/lru-cache": {
"version": "11.5.2",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.5.2.tgz",
"integrity": "sha512-4pfM1Ff0x50o0tQwb5ucw/RzNyD0/YJME6IVcStalZuMWxdt3sR3huStTtxz4PUmvZfRguvDejasvQ2kifR11g==",
"license": "BlueOak-1.0.0",
"engines": {
"node": "20 || >=22"
}
},
"node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"license": "MIT"
},
"node_modules/nofilter": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/nofilter/-/nofilter-3.1.0.tgz",
"integrity": "sha512-l2NNj07e9afPnhAhvgVrCD/oy2Ai1yfLpuo3EpiO1jFTsB4sFz6oIfAfSZyQzVpkZQ9xS8ZS5g1jCBgq4Hwo0g==",
"license": "MIT",
"engines": {
"node": ">=12.19"
}
},
"node_modules/process": {
"version": "0.11.10",
"resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz",
"integrity": "sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==",
"license": "MIT",
"engines": {
"node": ">= 0.6.0"
}
},
"node_modules/readable-stream": {
"version": "4.7.0",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-4.7.0.tgz",
"integrity": "sha512-oIGGmcpTLwPga8Bn6/Z75SVaH1z5dUut2ibSyAMVhmUggWpmDn2dapB0n7f8nwaSiRtepAsfJyfXIO5DCVAODg==",
"license": "MIT",
"dependencies": {
"abort-controller": "^3.0.0",
"buffer": "^6.0.3",
"events": "^3.3.0",
"process": "^0.11.10",
"string_decoder": "^1.3.0"
},
"engines": {
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
}
},
"node_modules/safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/string_decoder": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz",
"integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==",
"license": "MIT",
"dependencies": {
"safe-buffer": "~5.2.0"
}
},
"node_modules/undici-types": {
"version": "8.3.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.3.0.tgz",
"integrity": "sha512-j375ScV60dom+YkPFIfTLcOiPxkN/buHz5GobjLhixFuANaNs3C9l4GmrWqejgXWJ7BbJcFYpTEUkS1Ge8bpZQ==",
"license": "MIT"
}
}
}

16
nodejs/package.json Normal file
View File

@@ -0,0 +1,16 @@
{
"name": "coap-hello-udp",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"start": "node server.js",
"client": "node client.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "Minimal CoAP-over-UDP hello world server with GET/POST routes",
"dependencies": {
"coap": "^1.6.0"
}
}

102
nodejs/server.js Normal file
View File

@@ -0,0 +1,102 @@
'use strict';
const coap = require('coap');
const PORT = Number(process.env.COAP_PORT) || 5683;
const HOST = process.env.COAP_HOST || '0.0.0.0';
// --- helpers ---------------------------------------------------------------
const TEXT = 'text/plain';
const JSON_FMT = 'application/json';
/** Split "/hello?name=Bob" into ["/hello", "name=Bob"]. */
function splitUrl(url) {
const i = url.indexOf('?');
const path = (i === -1 ? url : url.slice(0, i)).replace(/\/+$/, '') || '/';
return [path, i === -1 ? '' : url.slice(i + 1)];
}
/** Pull a name out of "?name=Bob" style queries. */
function nameFromQuery(query) {
return new URLSearchParams(query || '').get('name');
}
/** Pull a name out of a POST body: raw text or {"name":"Bob"}. */
function nameFromBody(buf) {
const body = buf.toString('utf8').trim();
if (!body) return null;
if (body.startsWith('{')) {
try {
const parsed = JSON.parse(body);
return typeof parsed.name === 'string' ? parsed.name.trim() : null;
} catch {
return null;
}
}
return body;
}
function greet(name) {
return `Hello, ${name && name.length ? name : 'World'}!`;
}
function send(res, code, payload, format = TEXT) {
res.code = code;
res.setOption('Content-Format', format);
res.end(typeof payload === 'string' ? payload : JSON.stringify(payload));
}
// --- routes ----------------------------------------------------------------
// key: "METHOD /path"
const routes = {
'GET /hello': (req, res) => {
send(res, '2.05', greet(nameFromQuery(req.query)));
},
'POST /hello': (req, res) => {
const name = nameFromBody(req.payload) || nameFromQuery(req.query);
if (!name) {
return send(res, '4.00', 'Bad Request: send a name as the payload');
}
send(res, '2.01', greet(name));
},
'GET /health': (_req, res) => {
send(res, '2.05', { status: 'ok', uptime: Math.round(process.uptime()) }, JSON_FMT);
},
};
// --- server ----------------------------------------------------------------
const server = coap.createServer({ type: 'udp4' });
server.on('request', (req, res) => {
const [path, query] = splitUrl(req.url);
req.query = query; // node-coap only gives us req.url on the server side
const key = `${req.method} ${path}`;
const handler = routes[key];
console.log(`${req.method} ${req.url} from ${req.rsinfo.address}:${req.rsinfo.port}`);
if (!handler) return send(res, '4.04', `Not Found: ${path}`);
try {
handler(req, res);
} catch (err) {
console.error('handler error:', err);
send(res, '5.00', 'Internal Server Error');
}
});
server.on('error', (err) => console.error('server error:', err));
server.listen(PORT, HOST, () => {
console.log(`CoAP server (UDP) listening on coap://${HOST}:${PORT}`);
console.log(' GET /hello?name=Imran');
console.log(' POST /hello (payload: Imran | {"name":"Imran"})');
console.log(' GET /health');
});
process.on('SIGINT', () => server.close(() => process.exit(0)));