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 databases.
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