Skip to content

How would one receive a json object, process it, then return Json back via http in asp.vNext? #787

Closed
@govpack

Description

@govpack

How would one receive a json object +[process with a dynamically loaded method /async task]
and then return the Json result back via http in asp.vNext?

It's a pretty common use case for an http server to be able to act as a json API... last night i cloned and browsed the source code of glimpse v2, to see how such a thing could be done (but there were way too many files and i didn't know which way was up) i google and i bing but i'm yet to see a small example that demonstrates such a thing in action.

for example here's how it could be done in 50 lines of javascript using dynamically loaded worker methods in •r:Ruby •v:JS •w:Clojure •x:Perl •y:Python •z:PHP

vx=require('vertx')
LOG=require('vertx/console').log
container=require('vertx/container')
DP=container.deployWorkerVerticle
l={a:LOG,j:JSON.stringify}
eb=vx.eventBus;
eb.registerHandler('godo',godo)
NUT={}
toolz={
fix:function(o){/*Ready o.[rx|rv|ry|rn] properties*/
if(typeof o.rx=='number'){o.rx=[o.rx.toString()]}
if(typeof o.rx=='string'){o.rx=o.rx.replace(/[\W\s]+$/,'').split('/');for(var i=0;i<o.rx.length;i++){if(/^-?\d+/.test(o.rx[i])){o.rx[i]=Number(o.rx[i])}}}
if(typeof o.rv=='undefined'){o.rv=''}
if(typeof o.ry=='undefined'){o.ry=[]}
if(typeof o.rn=='undefined'){o.rn=0}
if(o.cb){/*jsonp*/o.rb=-3}
if((''+o.rx[0]).charAt(0)=='~'){o.rb='-'+o.rx[0].substr(1);o.rx.shift()}else if(!o.rb){o.rb=o.all?-2:-1}
}
}
var N=0

function godo(o,cb){/*Run o thru one or more verticles*/
LOG(o.rn+' von.js received: ' + l.j(o));o.rb=++N
NUT[o.rb]=(function(cb){return function(o){cb(o)}})(cb)
toolz.fix(o)
DoNext(o)
} 

function DoNext(o){LOG(o.rn)
if(o.rn!=0){o.e=o.rv}
if(o.rn>=o.rx.length){LOG('returning: ' + l.j(o));NUT[o.rb](o);/*delete NUT[o.rb]*/;return}
var ns=''+o.rx[o.rn];var s=ns.charAt(0);var n=parseInt(ns.substr(1))
LOG(o.rn+'/'+o.rx.length+' ~ '+ns+'['+n+']'+s)
o.rn+=1;
var ext=({r:'rb',v:'js',w:'clj',x:'pl',y:'py',z:'php'})[s]
LOG('EXT:'+ext)
if(!ext){NUT[o.rb](o);delete NUT[o.rb];return}
if(s=='v' && n==3){var RV='fail';try{eval('RV=('+o.e+')')}catch(er){RV=er.message};LOG(RV);o.rv=RV;return DoNext(o);}
if(NUT[ns]){LOG(ns+' already loaded');return eb.send(ns,o,DoNext)}
var fpr='../../../../2/'+s+s+'/Ax/'+n+'/'+s+'.'+ext;LOG('ns:'+ns);LOG('ns:'+fpr)
container.deployVerticle(fpr,null,1,function(){NUT[ns]=true;eb.send(ns,o,DoNext)})
}

web1=vx.createHttpServer()
sox=vx.createSockJSServer(web1)
sox.bridge({prefix:'/eventbus'},[{}],[{}])
web1.listen(1422)

this is the same question as: #770
except that one shows "Powershell Listening on http://127.0.0.1:1416..."
getting and returning Json via http

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions