Why do different node.js sessions share 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? They are different sessions so I expect node.js use different child processes for them. My understanding, with PHP, is that sharing variables should be implemented using databas开发者_如何学运维es.
Why can node.js do this without any external storage? Is it a single-process but multiple threads?
How do I declare a variable that belongs to a specific session?
Node.js is single process.
Your code runs in a single process ontop of an eventloop.
JavaScript is single threaded. Every piece of code you run is single threaded. Node.js is fast and scales because it does not block on IO (IO is the bottle neck).
Basically any javascript you run is single threaded. JavaScript is inherantly single threaded.
When you call parts of the nodeJS API it uses threading internally on the C++ level to make sure it can send you the incoming requests for the HTTP servers or send you back the files for the file access. This enables you to use asynchronous IO
As for 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++;
});
The 'problem' you're seeing isn't specific to node. It's just a function of scoping in javascript. You've declared your variable under a scope that lives for the lifetime of the server, not the request.
Declaring your variable within the function used to respond to 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);
node.js is essentially single-threaded, actually.
Each request to a PHP script is handled by a separate PHP instance, but it executes within the same server process (often Apache).
A node.js script is both the server and the handler in one. Since the state is held by the server, it is preserved between requests. For long-term persistence you would want to use a database as in PHP. However, if you want to implement a chat server or something where long-term memory isn't important, keeping it all in the server's memory can simplify things.
Node.js is single threaded, which can reduce the overhead of child process/thread creation. And it takes use of asyn functions and callbacks, so that Node can handle other requests when the previous one is blocked, and provides good performance when the app focuses on heavy data traffic but not computation.
It is a little bit like the concept of functional programming, you need to handle variable carefully.
You should use closure to create a space for each request if you wanted. But keep in mind that closure cannot be optimized by JS engine easily.
精彩评论