开发者

Nodejs in-memory storage [closed]

Closed. This question needs to be more focused. It is not currently accepti开发者_高级运维ng answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 9 days ago.

The community reviewed whether to reopen this question 9 days ago and left it closed:

Original close reason(s) were not resolved

Improve this question

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


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/');


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


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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜