Skip to content

An introduction to run http over udp with node.js

tomzhou edited this page Nov 18, 2020 · 3 revisions

nodejs-httpp is try to run HTTP traffic over UDP with Node.js

  • API is compatible to HTTP. Just require('httpp') instead of http, when you coding.

  • The HTTPP to HTTP modules mapping is like below: net -> udt, http -> httpp, tls -> udts, https -> httpps.

UDT echo server/client example:

echo_srv.js:

 var udt = require('udt');
 var srv = udt.createServer(function(socket){
    socket.pipe(socket);     
 });
 srv.listen(51686);
 console.log('Listening on UDP port 51686');

echo_cln.js

 var udt = require('udt');
 var cln = udt.connect({port:51686} , function(){
    console.log('you can type char here, then server send it back:\n');
    process.stdin.resume();
    process.stdin.pipe(cln);   
    cln.pipe(process.stdout); 
 });

HTTPP server/client example:

httpp_srv.js

 var httpp = require('httpp');
 var srv = httpp.createServer(function(req, res){
   res.end('Hi, just say hi to you over UDP ...\n');
 });
 srv.listen(51688);
 console.log('HTTPP server listing on UDP port 51688');

httpp_cln.js

 var httpp = require('httpp');
 httpp.get('http://localhost:51688', function(res){
   console.log('STATUS: ' + res.statusCode);
   console.log('HEADERS: ' + JSON.stringify(res.headers));
   res.on('data', function (chunk) {
     console.log('BODY: ' + chunk);
  });
 });
Clone this wiki locally