0

I have a node JS backend and it is returning status code 304 when answering the first request I make to that backend each time I restart my application. I have read about status code 304 and supposedly it means that the content that the request is fetching hasn't changed, but, since I get 304 on the first request my front end does not really have any useful data to deal with precisely because it is the first request ever made since the app started. I have tried to disable the e-tag and stuff like that but in reality nothing helps because I can't get any data on the first request.

Node JS code below:

router.get('/results', async function(req,res){
    const authHeader = req.headers.authorization;
    const results = [];
    for (let i = 1; i < 11; i++) {
        results.push({evaluation: i, total: 0, equipamentos: []});
    }

    if(authHeader){
        const token = authHeader.split(' ')[1];
        await jwt.verify(token, accessTokenSecret, async (err, user) =>{
            if(err){
                return res.sendStatus(403)
            }
            const db_form = client.db("mavideobo").collection("form");
            await db_form.find( {owner: jwt.decode(token).username}).toArray(function (err, result) {
                if (err) {
                    res.send(err);
                } else {
                    result.forEach(formAnswer => {
                        results[formAnswer.Evaluation - 1].total++;
                    })
                }
            })
            res.status(200).json(results);
        })
    }else{
        res.sendStatus(401);
    }
})

React function below:

const getResultsData = async () => {
        setData([]);
        await fetch("/api/form/results", {
            headers: {
                Authorization: `Bearer ${sessionStorage.getItem("authToken")}`,
                "Content-Type": 'application/json',
                "Accept": 'application/json'
            }
        }).then(response => {response.json() 
            console.log(response)})
            .then(res => {
                let listResults = [];
                for (let i in res) {
                    listResults.push(res[i]["total"])
                }
                setData(listResults);
            }).catch(function (error) {
            console.log(error);
        });
    }
João Dias
  • 45
  • 1
  • 8

0 Answers0