Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is node.js express-session stateless or stateful?

I am actually working with express-session and I have a question : Is node.js, or more particularly express-session sateless or stateful ?

In fact, when we need to use session with node, we use the req.session.foo, it means that we use the request to get the session.

So, is there anything stored on the server (stateful) ? In this case, why cant we access the value with a "global variable" like in PHP ?

In the other case, it means that we have a stateless server, and is the sessionID value correspond to the token key ?

Can you help me understanding this middleware a bit more ?

Thanks for advance

like image 930
mfrachet Avatar asked May 01 '26 22:05

mfrachet


2 Answers

is there anything stored on the server

Yes, that is how sessions work

why cant we access the value with a "global variable" like in php

Because a global in a Node based HTTP server is global for the server and not the the request. You'd have sessions overwriting each other.

like image 102
Quentin Avatar answered May 03 '26 10:05

Quentin


Sessions are about storing information about a specific client inside the server, so you can't talk about a strictly "stateless server" anymore.


The "global variables" in PHP are also per-request.

Note that PHP code is invoked once per request, while Node code keeps running and serves multiple requests, so you need to explicitly take a specific request's session.

like image 25
Kos Avatar answered May 03 '26 10:05

Kos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!