Express JS Router: persisting data

I have some "router" modules being exported into my app.js from a people.js & auth.js file.

Both of these router modules (people.js & auth.js) are require()'ing an array of objects called people, which is being module.export'd out of data.js.

Each object inside this people array represents a person', each holding an 'id' and a 'name' property.

Inside of my people.js router, one of its routes... the router.put().. allows me to change the "name" property of any of the people objects, and if successful returns a json with the new name (pic2).

Separately, inside the other auth.js router, which also require()'s that same people array, I have a single route (router.post()), which tests the req.body "name" property against all of the "people" object name properties, looking for a match, and then res.send()'s a simple 'welcome name' if a match is found... (comment pic)

I'm testing these routes inside Postman.

When I run the router.put() route from people.js, changing the name ppty in one of the 'people' objects, I'll immediately afterward run another router.get() request from people.js, which returns the people array of people objects. (pic 3), and I'll see the name-change from the router.post() request reflect in the people array. The changed name is seen in the list of objects.

However, if I then run that auth.js router.post() where it tests people for a name match and sends back a 'welcome name', it does not recognize the changed name, and the .status(401) is returned.

So it's almost like, so long as I continue to make requests from the same router (whether people.js, or auth.js), then any changes to the object in the require()'d people array will 'persist' from one request to another..

But if I make a change to that array in one router (say with a people.js route), that change to people won't persist for a auth.js route
Screen_Shot_2023-05-31_at_11.37.07_AM.png
Screen_Shot_2023-05-31_at_11.51.41_AM.png
Screen_Shot_2023-05-31_at_12.04.06_PM.png
Was this page helpful?