Sunday, 7 May 2017

assigning res.locals.object to app.locals.object and then changing the value of the res.locals object changes the app.locals object

I am implementing "modes" on a website, which I'm doing by creating an app.locals.page object which has a mode property and by default it is simply "default". We're using express4 and pug for this :)

then there is an app.get router with the paramater ":mode" like so

app.get('/(|modes.:mode)', ..)

which we then catch via

function(req, res){
    if(!req.params.mode){
        res.locals.page = app.locals.page
        res.render('ourPage')
    }
    else {
        res.locals.page = app.locals.page
        res.locals.page.mode = req.params.mode
        res.render('ourPage')
    }

But upon testing, as soon as I got to site.dev/modes.mode and then back to site.dev/, it seems the mode is sticking to the app.locals.page somehow and so on loading a page without a mode preference in the URL it loads the newly edited mode property from app.locals.page as the mode. However nowhere do we assign the app.locals.page.mode to anything, we only use it's page property to assign the temporary res.locals.page object for the page we're loading. So what is going on here?

Super weird.

Thank you



via lopu

No comments:

Post a Comment