views:

390

answers:

3

How do i store data to be used for all the clients in my server? (like the messages of a chat)

+6  A: 

The server that node.js allows you to build, is an application server, which means that state is preserved, between request, on the server side. The following snippet demonstrates this:

var sys  = require('sys'),
    http = require('http');

var number = 0;

http.createServer(function (req, res) {

        res.sendHeader(200, {'Content-Type': 'text/html'});
        res.sendBody('<h1>Number is: ' + number + '</h1>');
        res.finish();

        number++;

}).listen(8000);

sys.puts('Server running at http://127.0.0.1:8000/');
Ionuț G. Stan
+2  A: 

If you want more features have a look at redis-node-client

Bharani
+1  A: 

Or use a native node storage mechanism (written in node.js)

http://github.com/felixge/node-dirty

christkv