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.

15
  • 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? Commented May 11, 2014 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! Commented May 11, 2014 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. Commented May 11, 2014 at 20:03
  • @moderns: That's a regular AJAX request. What did you try? What are you having trouble with? Commented May 11, 2014 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. Commented May 11, 2014 at 20:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

There is no reason to use JSONP here. And lack of SOP is a security issue, not an advantage.
Would you please post a full simple code? Thanks.
@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.
@moderns google jquery ajax (for you I'd recommend using jquery): $.ajax(path_to_server_endpoint, { success: callback })
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.

3 Comments

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.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.