0

There are 3 components to my problem.

  1. Declaring a server side (node.js) variable to hold data for the duration of the server run - just a number.
  2. Sending a number to the server from the client (jQuery) to update the server value.
  3. Sending a number back to the client as calculated by the server from the new number.

I haven't been successful creating a variable I can access - I have tried a separate .js file with export.global, as well as var sum = require('./sum');

My best guess to send the data to the server is $.post ('/sum', 'entry=20', showNewTotal);.

I can also get it on to the HTML display in showNewTotal $('#currentSum').text('data');

This seems like it should be simple, however I haven't been able to identify any examples.

1 Answers1

0

Your sum module could look like this:

module.exports = {sum:0};

To set anywhere, you do:

var sum = require('./sum');
sum.sum = 10;

To retrieve it anywhere else, you do:

var sum = require('./sum');    
console.log(sum.sum);

So to set it with $.post, you'll need a route set up like adeneo mentioned using app.post('/sum') with a handler that does the set I wrote by grabbing the entry param and then sending back an object like {new_total:sum.sum} that would be used by your showNewTotal function.

  • Solved it in combination with [link](http://stackoverflow.com/questions/18540965/node-express-post-route-throwing-error-expected-callback-got-object), got object" post - Thanks! – user2836959 Nov 18 '13 at 00:27