Why are different node.js variables sharing variables?

Here is a simple program:

var express = require('express'); var app = express.createServer(); var count = 0; app.get("/", function(req, res) { res.send(count.toString()); count++; }); app.listen(3000); 

When I open it in two different browsers, the first displays 0 and the second displays 1 .

Why? These are different sessions, so I expect node.js to use different child processes for them. My understanding, with PHP, is that the exchange of variables must be implemented using databases.

Why does node.js do this without external storage? This is one process, but multiple threads?

How to declare a variable related to a specific session?

+9
express
Jun 15 '11 at 13:00
source share
4 answers

Node.js is one process.

Your code runs in a single ontop eventloop process.

JavaScript is single threaded. Each piece of code that you run is single-threaded. Node.js is fast and scalable because it does not block on IO (IO - bottleneck).

Basically, any javascript that you run is single-threaded. JavaScript is essentially single-threaded.

When you call parts of the nodeJS API, it uses streams internally at the C ++ level to make sure that it can send you incoming requests for HTTP servers or send you files to access the files. This allows the use of asynchronous IO

As for the sessions

 app.use(express.session({ secret: "Some _proper_ secret" })); ... app.get("/", function(req, res) { if (req.session.count == null) { req.session.count = 0; } res.send(req.session.count); req.session.count++; }); 
+10
Jun 15 2018-11-11T00:
source share

The "problem" you see does not apply to node. This is just a javascript scope function. You have indicated your variable under the scope, which lives throughout the life of the server, not the request.

Declaring your variable in the function used to answer your route will solve your problem:

 var express = require('express'); var app = express.createServer(); app.get("/", function(req, res) { var count = 0; res.send(count.toString()); count++; }); app.listen(3000); 
+10
Jul 24 2018-11-11T00:
source share

node.js is actually single threaded.

Each PHP script request is processed by a separate PHP instance, but it is executed within the same server process (often Apache).

A node.js script is both a server and a handler in one. Since the state is maintained by the server, it is saved between requests. For long-term preservation you would like to use a database, as in PHP. However, if you want to implement a chat server or something where long-term memory is not important, then all this in the server’s memory can simplify.

+2
Jun 15 2018-11-11T00:
source share

Node.js is single-threaded, which can reduce the overhead of creating child processes / threads. And it uses asyn functions and callbacks, so Node can handle other requests when the previous one is locked, and provides good performance when the application focuses on heavy data traffic, but not computation.

This is a bit like the concept of functional programming, you need to carefully handle the variable.
You must use closure to create space for each request if you want. But keep in mind that closure cannot be optimized with a JS engine.

0
Feb 18 '13 at 8:46
source share



All Articles