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.