简体   繁体   中英

Weird (caching) issue with Express/Node

I've built an angular/express/node app that runs in google cloud which currently uses a JSON file that serves as a data source for my application. For some reason, (and this only happens in the cloud) when saving data through an ajax call and writing it to the json file, everything seems to work fine. However, when refreshing the page, the server (sometimes!) sends me the version before the edit. I can't tell whether this is an Express-related, Node-related or even Angular-related problem, but what I know for sure is that I'm checking the JSON that comes in the response from the server, and it really is sometimes the modified version, sometimes not, so it most probably isn't angular cache-related.

The GET :

router.get('/concerts', function (request, response) {
    delete require.cache[require.resolve('../database/data.json')];
    var db = require('../database/data.json');
    response.send(db.concerts);
});

The POST :

router.post('/concerts/save', function (request, response) {
    delete require.cache[require.resolve('../database/data.json')];
    var db = require('../database/data.json');
    var concert = request.body;
    console.log('Received concert id ' + concert.id + ' for saving.');
    if (concert.id != 0) {
        var indexOfItemToSave = db.concerts.map(function (e) {
            return e.id;
        }).indexOf(concert.id);
        if (indexOfItemToSave == -1) {
            console.log('Couldn\'t find concert with id ' + concert.id + 'in database!');
            response.sendStatus(404);
            return;
        }
        db.concerts[indexOfItemToSave] = concert;
    }
    else if (concert.id == 0) {
        concert.id = db.concerts[db.concerts.length - 1].id + 1;
        console.log('Concert id was 0, adding it with id ' + concert.id + '.');
        db.concerts.push(concert);
    }
    console.log("Added stuff to temporary db");
    var error = commit(db);
    if (error)
        response.send(error);
    else
        response.status(200).send(concert.id + '');
});

This probably doesn't say much, so if someone is interested in helping, you can see the issue live here . If you click on modify for the first concert and change the programme to something like asd and then save, everything looks fine. But if you try to refresh the page a few times (usually even up to 6-7 tries are needed) the old, unchanged programme is shown. Any clue or advice greatly appreciated, thanks.

To solve: Do not use local files to store data in cloud! This is what databases are for!

What was actually the problem?

The problem was caused by the fact that the App Engine had 2 VM instances running for my application. This caused the POST request to be sent to one instance, it did its job, saved the data by modifying its local JSON file, and returned a 200. However, after a few refreshes, the load balancing causes the GET to arrive at the other machine, which has its individual source code, including the initial, unmodified JSON. I am now using a MongoDB instance, and everything seems to be solved. Hopefully this discourages people who attempt to do the same thing I did.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM