NodeJS:您可以使用纯JavaScript对象作为内存数据存储吗?

而不是使用像Redis,甚至LokiJS(这看起来不错),我可以在内存中创build一个大型的JavaScript集合作为我的应用程序运行,并查询呢?

我有一个使用socket.io确切模式的应用程序。

贝娄,我已经翻译我的socket.io代码使用HTTP请求。

在服务器上,你可以做这样的事情:

var players = []; //This will be used to store in-memory players. 

他们,

 var express = require('express'); var app = express(); var bodyParser = require('body-parser'); app.use(bodyParser.json()); //Need this to populate req.body on requests. app.all('*', function (req, res, next) { res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); next(); //Need this to accept incoming requests. }); app.get('/players', function (req, res) { res.json(players); }); app.post('/players', function (req, res) { //validation players.push(req.body); }); [...] 

看看这个例子: http : //agar.io/

这是一个非常受欢迎的游戏。 现在,他们为什么要在数据库上玩你的位置,分数或名字? 这将是非常昂贵的。

Interesting Posts