0

Is there any way to execute Javascript code at client side (browser) from Node.js server without the use of Socket.io? Something like Callback function to get the response from the Node.js server to be executed at the browser which launched the request to node.js server?

In Normal PHP/AJAX/Javascript communication, when sending a request to PHP server then there is a callback function and the xmlHTTP.responseText will give us a response from server to take action on the browser! I wonder if there is something similar in Node.js?

UPDATE

Node Server.js

var http=require('http');
var mysql = require('mysql');
var util = require('util');
var url = require('url');

var connection = mysql.createConnection({
   host     : 'localhost',
   user     : 'xxx',
   password : 'xxx',
   database : 'db_test',  //mysql database to work with (optional)
});
connection.connect(); //connect to mysql

var server=http.createServer(function(req,res){

    test(res);
});


server.on('listening',function(){
    console.log('ok, server is running');
});

server.listen(9000);


function test(res){
    var s = new Date().getTime();
    connection.query('INSERT INTO table_test.emails (email) VALUES   ("'+s+'")',function(err, result) {});

    res.end('{id:db_id, time:timestamp}');
}

Browser (client side) call:

ajax.call({
    url: 'example.com/node',
    method: 'post',
    callback: function(){
        alert('Response received from node.js server');
    } 
});

I need to send a response from node server.js to be executed at the browser in the callback function. How can I achieve that?

Can I use eval in javascript to eval a code at server? I need to output from server like this: var response = {id:db_id, time: timestamp, etc..}

So I get response at browser and render the result there.

I would greatly appreciate if you provide a simple example.

Thanks.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
moderns
  • 650
  • 10
  • 23
  • 1
    Node.js is server-side code. Your client-side Javascript can't even tell the difference between an AJAX request to a Node.js server and a PHP server. What have you tried? What isn't working? – SLaks May 11 '14 at 19:59
  • Is there any way to have a callback function? I need to get result from the node.js server to execute it on the web browser. That's why I called the server to get result from database and render it to browser! – moderns May 11 '14 at 20:02
  • @moderns yes, whatever you did with php should work in node, only it's the same language on the client and server. That's what SLaks said there. – Benjamin Gruenbaum May 11 '14 at 20:03
  • @moderns: That's a regular AJAX request. What did you try? What are you having trouble with? – SLaks May 11 '14 at 20:03
  • @BenjaminGruenbaum Would you please post a simple code? I am new in Node.js and didn't find examples on internet. Thanks. – moderns May 11 '14 at 20:05
  • @moderns are you using express? How does your current code look like for serving the request? It's very unclear what specific detail you're stuck on. – Benjamin Gruenbaum May 11 '14 at 20:06
  • I am using the Javascript at browser to call the server as normal AJAX call, I need to get the response from the node.js server. – moderns May 11 '14 at 20:07
  • @moderns show us your _current_ server side and client side code for your AJAX call (or better, a self contained short sample of similar code), and explain to us where you want the function to execute. – Benjamin Gruenbaum May 11 '14 at 20:09
  • @moderns: The same way you get the response from any other AJAX call. **What did you try? What are you having trouble with?** – SLaks May 11 '14 at 20:15
  • Would you please check my update? I have added the code for server node.js and the browser code. I don't know how to send the response from server to be read at the client.. – moderns May 11 '14 at 20:16
  • You need to write to the `res` object to send a reply. See the documentation, and look at http://expressjs.com/ – SLaks May 11 '14 at 20:18
  • Thanks, do you mean res.end({id:db_id, time:timestamp, ...});, Like this? – moderns May 11 '14 at 20:21
  • You need to pass a string (typically JSON). Or use Express, which does that for you. – SLaks May 11 '14 at 20:23
  • From where to send it? I need it to result from test function as I will collect data from database. Sorry if my question is silly but never used node.js before.. – moderns May 11 '14 at 20:25
  • You need to understand what HTTP is, and to read documentation or tutorials on Node.js HTTP servers. – SLaks May 11 '14 at 20:28

2 Answers2

0

One option is to insert a script element in your document with the src attribute set to the desired server request, and have the response contain a single function call, generally with response data as an argument. This is known as "JSONP". Beware that cross-origin policies do not apply to these requests.

Another option is a traditional AJAX request. From your question, I don't understand why this isn't the natural solution.

Abram
  • 413
  • 1
  • 3
  • 13
  • 1
    There is no reason to use JSONP here. And lack of SOP is a security issue, not an advantage. – SLaks May 11 '14 at 20:04
  • Would you please post a full simple code? Thanks. – moderns May 11 '14 at 20:04
  • @SLaks, you're correct on both accounts. I edited to reflect that. I guess I said it was a benefit because generally JSONP is used to get around cross origin policies. – Abram May 11 '14 at 20:09
  • @moderns google jquery ajax (for you I'd recommend using jquery): $.ajax(path_to_server_endpoint, { success: callback }) – Abram May 11 '14 at 20:12
0

I never expected to be easy. At server side, the response will be sent in the format:

res.end('{id:db_id, time:timestamp}');

At browser side, I receive and execute the request through:

eval(xmlHttp.responseText);

Thanks for everyone participated.

moderns
  • 650
  • 10
  • 23
  • I imagine you want JSON.parse and not eval. JSON.parse will parse any valid JSON string, where eval will execute arbitrary JS code. In this particular case it probably doesn't matter, but if any portion of the data comes from an untrusted source, you definitely will want to use JSON.parse. – Abram May 11 '14 at 21:48
  • Thanks but JSON.parse doesn't execute all Javascript codes. If I want to execute alert('hello') coming from the response, JSON.parse wont work :( Please clarify – moderns May 11 '14 at 22:34
  • http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – Jorge Aranda May 12 '14 at 04:54