Persisting objects after AJAX calls in Node.js

How can objects that are created during an AJAX request persist so that it can be used by subsequent AJAX calls?

I have tried using the debugger to see if my variables exist after a call to my route function and they seemed to be destroyed each time after the call ends.

Any suggestions or examples?

First of global variables are bad. I think what you are looking for is called sessions. Using web framework express this is achieved without any pain.

package.json

{
  "name": "hello-world",
  "description": "hello world test app",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "express": "3.x"
  }
}

app.js

var express = require('express');
var app = express();
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.cookieParser('keyboard cat'));
app.use(express.cookieSession());

app.get('/count', function (req, res) {
    if (req.session.count === undefined) {
        req.session.count = 1;        
    } else {
        req.session.count += 1;
    }

    res.send('Number of times you visited this page: ' + req.session.count);
});

app.get('/reset', function (req, res) {
    req.session = null;

    res.send('session has been reset');
});

app.listen(3000, '127.0.0.1');
console.log('Listening on port 3000');

After you have created the file package.json you can install express issuing npm install. Then running program is as simple as node app.js. The route htttp://localhost:3000/counter counts how many times you have visited that page. Even after you have shutdown server. The route http://localhost:3000/reset is used to reset the counter.

What I was looking for was the global object.

To use foo between different modules and router calls, I declared global.foo and used that object.

http://nodejs.org/api/globals.html