Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8133891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:48:24+00:00 2026-06-06T09:48:24+00:00

I am writing a node app that needs to remember data across connection iterations

  • 0

I am writing a node app that needs to remember data across connection iterations of the createServer() callback. Is there a simple way that doesn’t involve databases or file r/w? I’ve sofar attempted creating objects in the respective modules and even main script while passing them into various response handlers, however for every connection they are flushed.

What I mean by that:

require('http').createServer(function(req,res){
        route(req,res,object);
}).listen(cp=80);

object={variable:0}

function route(req,res,object){
    res.end();
    console.log(object.variable);
    object.variable=Math.floor(Math.random()*100);
}

console.log is unsurprisingly throws 0 every connection in this case. Is there any way to create global variables, not in the sense of being available across modules, but persistent unlike var‘s?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:48:25+00:00Added an answer on June 6, 2026 at 9:48 am

    Each module in Node has its own scope, so no, var Foo; does not create a global variable Foo. Use global object from inside the modules.

    UPDATE:

    require('http').createServer(function(req,res){
            route(req,res,object);
    }).listen(cp=8080);
    
    object={variable:0}
    global.foo = 'Bar'; // I added this
    
    function route(req,res,object){
        res.end();
        console.log(object.variable);
        console.log("foo = %s", global.foo); // I added this too
        object.variable=Math.floor(Math.random()*100);
    }
    

    And it logs “foo = Bar” as expected as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a ruby app that stores ancestry type data. I.e. Family tree. For
I'm writing a jQuery app that interfaces with a node.js server, so I'd like
I am writing a simple test app to experiment with the functionality of node.js
I'm currently writing a simple cross platform app with Node.js on the server and
I'm writing a node.js app and the most curious thing is happening when I
A web app I am writing in JavaScript using node.js. I use Foreman, but
I'm writing an app that parses a very large logfile, so that the user
I'm using coffeescript while writing a node js app and use cake watch to
I'm writing an app that's destined for the browser, and I want to write
I am writing a business app in Flex that uses web services to communicate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.