Node.js error in Edge

Not applicable

Hi @Dino @Ozan Seymen

I have a simple node app proxy:-

var express=require('express');

var app = express();

x=0;

y=0;

app.get('/servicex', function (req, res) { res.header('x',x); res.send(200); });

app.put('/servicex', function (req, res) { x=x+1; res.send(200); });

app.listen(3000, function () { console.log('Example app listening on port 3000!') });

This code is working as expected in my local node environment.

I'm trying to synchronise the counter but GET is always returning 0. Please advise.

Please advise.

0 2 126
2 REPLIES 2

Maybe you need to properly declare x. When I use var x = 0, I don't see that behavior. I used this node.js server:

var express=require('express');
var app = express();
var x=0;

app.get('/servicex', function(request, response) {
  response.header('x',x)
    .status(200)
    .send({ message: 'ok' });
});

app.put('/servicex', function (request, response) {
  x=x+1;
  response.header('Content-Type', 'application/json')
    .status(200)
    .send({ value: x });
});

// default behavior
app.all(/^\/.*/, function(request, response) {
  response.header('Content-Type', 'application/json')
    .status(404)
    .send('{ "message" : "This is not the server you\'re looking for." }\n');
});

app.listen(3000, function () { console.log('Example app listening on port 3000!'); });


And these are my results:

$ curl -i https://ORGNAME-ENV.apigee.net/nodecounter/servicex 
HTTP/1.1 200 OK
Date: Sun, 12 Mar 2017 17:44:21 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 16
Connection: keep-alive
x: 0
ETag: W/"10-/VnJyQBB0+b7i4NY83P42KKVWsM"
Server: Apigee Router


{"message":"ok"}




$ curl -i https://ORGNAME-ENV.apigee.net/nodecounter/servicex 
HTTP/1.1 200 OK
Date: Sun, 12 Mar 2017 17:44:30 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 16
Connection: keep-alive
x: 0
ETag: W/"10-/VnJyQBB0+b7i4NY83P42KKVWsM"
Server: Apigee Router


{"message":"ok"}




$ curl -i https://ORGNAME-ENV.apigee.net/nodecounter/servicex -X PUT -d ''
HTTP/1.1 200 OK
Date: Sun, 12 Mar 2017 17:44:44 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 11
Connection: keep-alive
ETag: W/"b-1G0mLy8XZBXyWbhE8ywefXPbwlQ"
Server: Apigee Router


{"value":1}




$ curl -i https://ORGNAME-ENV.apigee.net/nodecounter/servicex -X PUT -d ''
HTTP/1.1 200 OK
Date: Sun, 12 Mar 2017 17:44:58 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 11
Connection: keep-alive
ETag: W/"b-8OvYfbKrxf256Wuww2Lo8Lyyc18"
Server: Apigee Router


{"value":2}




$ curl -i https://ORGNAME-ENV.apigee.net/nodecounter/servicex -X PUT -d ''
HTTP/1.1 200 OK
Date: Sun, 12 Mar 2017 17:45:08 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 11
Connection: keep-alive
ETag: W/"b-mfvdUB8v1Og+zFCdDsgYMf4scWw"
Server: Apigee Router


{"value":3}




$ curl -i https://ORGNAME-ENV.apigee.net/nodecounter/servicex 
HTTP/1.1 200 OK
Date: Sun, 12 Mar 2017 17:45:26 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 16
Connection: keep-alive
x: 3
ETag: W/"10-/VnJyQBB0+b7i4NY83P42KKVWsM"
Server: Apigee Router


{"message":"ok"}


Thanks @Dino

The only problem i see is that the counter is not synchronizing consistently over multiple Put/Get calls.

I was thinking as Node.js is a single thread language so the value would highly consistent over multiple http calls without me having to do anything special.

Please advise.