0

I would like to be able to post user-generated JSON objects to a NodeJS server, and store them in an array so that refreshing the page does not affect the data. I would then like to be able to read the info back into a table, so that it constantly reflects the contents of the array.

Is this possible in NodeJS? If so, could someone please give me a pointer in the right direction? I'm able to pass JSON objects (as req.body), but I don't know where to go from here.

Thanks!

2
  • If you need persistent data storage, you will need to store the "JSON objects" into a database. My suggestion for a DB engine would be MongoDB. I have used it in the past with NodeJS. Commented Feb 4, 2014 at 21:16
  • Yes, something like this is possible. There are many different ways to do this, so i'd suggest trying a few and refining your question. One approach would be to store this in a session, (e.g. using the sessioning built into [express](using expressjs.com) and then store that session. You could then use redis or mongo to store and persist those sessions over a server restart. Commented Feb 4, 2014 at 21:19

1 Answer 1

1

As Sri suggested, you should definitively store your data array into a data base. Use either Redis, Mongo, MySQL... It entierly depends on your data shape and the ways you will use it.

If you just neet to store temporary an array (by temporary, I mean "as long as the browsing session" or "as long as the server is up"), you can just store you data in memory.

For exemple with ExpressJs:

var express = require('express');
// in memory storage
var data = [];


var app = express()
  .use(express.bodyParser())
  .use(express.cookieParser())
  .use(express.cookieSession({secret:'sample'}))

  .post('/data', function(req, res){
    // do not need session, just memmory ? use this line
    // data.push(req.body);
    // creates the session if needed, and store data
    if (!req.session.data) {
      req.session.data = [];
    }
    req.session.data.push(req.body);
    res.send(204);
  })

  .get('/data', function(req, res){
    res.setHeader('Content-Type', 'application/json');
    // stored in session ?
    res.send(req.session.data || []);
    // stored in memory ?
    // res.send(data);
  })

  .listen(80);

----- EDIT -----

To add new data and return it in a single request, just send your array in the POST response:

  .post('/data', function(req, res){
    // store in memory
    data.push(req.body);
    // send it back, updated, in json
    res.setHeader('Content-Type', 'application/json');
    res.send(data);
  })

There is an assumption: you should call your post with an ajax call, and not a regular HTML form. Please refer to jquery.Ajax doc to get the parsed server result.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This is more or less what I'm looking for from a storing perspective -- memory should do just fine. However, I'm still a little unclear on how I can read the data back from the server-side array into my JavaScript page. Ideally, a single button click on the client-side should POST the data to the array and read back the entire array into a table (updating it, effectively). Any ideas?
If it fits your need, would you mind closing your question ? thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.