NodeJS:您可以使用普通Javascript对象作为内存中的数据存储吗?

NodeJS:您可以使用普通Javascript对象作为内存中的数据存储吗?,javascript,node.js,redis,in-memory-database,lokijs,Javascript,Node.js,Redis,In Memory Database,Lokijs,我可以在应用程序运行时,在内存中创建一个大型javascript集合,并查询它,而不是使用Redis,甚至LokiJS(看起来很棒)之类的工具吗?我有一个应用程序,它使用socket.io使用这种模式 下面,我已将socket.io代码翻译为使用HTTP请求 在服务器上,您可以执行以下操作: var players = []; //This will be used to store in-memory players. 他们, var express = require('express')

我可以在应用程序运行时,在内存中创建一个大型javascript集合,并查询它,而不是使用Redis,甚至LokiJS(看起来很棒)之类的工具吗?

我有一个应用程序,它使用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);
});

[...]
看看这个例子:


这是一款非常受欢迎的游戏。现在,为什么他们会在玩游戏时将你的位置、分数或名字存储在数据库中?这将非常昂贵。

是的,没有什么不好的。这很好地回答了您的问题: