Node single thread question?

since node is single-threaded, if ten users ask the server to query and modify the same data at the same time, node can only handle one request and execute it one by one.

let a="test"
router.get("/test/match",function(req,res){ 
      /* a*/
    }

 

so how does node manage to keep requests from interfering with each other? Because it is queued, after the above global variable an is modified, the value of the next request a variable has changed, which will affect the next request processing.

Mar.28,2021

in fact, in any case, the use of global variables should be reduced. Pure functions can be used to avoid this problem


variables related to user status can be placed in cookie. Node can also make full use of multicore systems: http://nodejs.cn/api/cluster.


the situation you mentioned exists.


node does have this problem, so you should pay special attention to it when dealing with it.
if you simply use your example and put it inside the callback function, it will not affect other users


router.get('/test/match',function(req,res){ 
    let a='test'
    /* a*/
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e469d3-4cf68.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e469d3-4cf68.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?