I am creating a REST api on top of express.js. I am having problems updating variables inside my routes.
Example:
I'm calling app.get("/wp/page/create/:id", function(req, res)
Inside this route, I start by invoking the http request using the library request-promise. I use the answer to this call in a nested http call.
I use a global variable for the headers for a nested call, and I need to make changes to the a header using a variable etag.
the code:
global.postHeaders = headers;
postHeaders['X-HTTP-Method'] = "MERGE";
postHeaders['Content-Type'] = 'application/json;odata=verbose';
postHeaders['X-RequestDigest'] = spContext;
request.get({
url: "xxx",
headers: headers,
json: true
}).then(function(response) {
var etag = response.d.__metadata.etag
postHeaders['If-Match'] = etag;
request.post({
url: "xxx",
type: "POST",
body: data,
headers: postHeaders,
json: true
}).then(function(data) {
res.send(data).end()
console.log("All done!");
})
})
When I started the server and started the route, everything will be fine. When i, when you try to hit it again, the variables are etagall the same, although they need to be updated.
If I restart the server, it works again on the first try, but with a second / third error.
, ?