Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs in-memory storage [closed]

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


2 Answers

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) {
        console.log(req.method, req.url);

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

        number++;

}).listen(8000);

sys.puts('Server running at http://127.0.0.1:8000/');
like image 176
Ionuț G. Stan Avatar answered Sep 12 '25 09:09

Ionuț G. Stan


node-cache package is currently the best for key value store and it allows synchronous as well as async storage/retrieval/deletion of keys.

npm link

like image 31
rnjai Avatar answered Sep 12 '25 08:09

rnjai