简体   繁体   中英

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.

Bellow, I have translated my socket.io code to use HTTP Requests.

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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