How to make multiple calls to nodejs from javascript/jquery/webpage -


i have written nodejs plugin in c++ , trying call within locally hosted webpage jquery. have read can't make direct call nodejs because of same origin policy should use jsonp. note: pretty new jquery, nodejs , json(p), feeling adventurous. using following code:

node_test.js:

var mylib = require("./mylibrary"); var http = require("http");  http.createserver(function(req, res) {     console.log("received request: " + json.stringify(req.url));     res.writehead(200, {"content-type": "text/plain"});     res.end('_testcb(\'{"message": "' + mylib.my_awesome_function() + '"}\')'); }).listen(1337); 

index.html:

index.html: <doctype html> <html>     <head>         <title>             nodejs test         </title>         <link rel='stylesheet' type='text/css' href='style.css'/>         <script type='text/javascript' src='jquery.js'></script>     </head>     <body>     <div id="test"></div>     <script>         $(document).ready(function() {             $.ajax({                 url: "http://127.0.0.1:1337/",                 datatype: "jsonp",                 jsonpcallback: "_testcb",                 cache: false,                 data: "test_data",                 timeout: 5000,                 success: function(data) {                     $("#test").append(data);                 },                 error: function(jqxhr, textstatus, errorthrown) {                     alert('error ' + textstatus + " " + errorthrown);                 }             });         });     </script>     </body> </html> 

if run node node_test.js , view index.html in browser function gets called. however, able execute multiple functions own nodejs library. thought of somehow parsing data send nodejs app, , run through big switch case, bit this:

var reply = ""; switch (data) {     case 1:         reply = mylib.my_awesome_function();         break;     case 2:         reply = mylib.my_cool_function();         break;     // repeat every function have } // send reply 

but not sure if intended way this. there better/smarter way make multiple calls nodejs within jquery/javascript on webpage?


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -