简体   繁体   English

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

[英]NodeJS: Can you use plain Javascript objects as an in-memory datastore?

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

I have an app with this exact pattern using socket.io. 我有一个使用socket.io的确切模式的应用程序。

Bellow, I have translated my socket.io code to use HTTP Requests. 在下面,我已经翻译了我的socket.io代码以使用HTTP请求。

On the server, you can do something like that: 在服务器上,您可以执行以下操作:

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

Them, 他们,

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);
});

[...]

Look at this example: http://agar.io/ 看这个例子: http : //agar.io/

This is a very popular game. 这是一个非常受欢迎的游戏。 Now, why would they store your position, score or name, while playing, on database? 现在,为什么他们在比赛时将您的位置,得分或姓名存储在数据库中? This would be very costly. 这将是非常昂贵的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM