Solved! Go to Solution.
You have several problems in that code.
var svr = http.createServer(function(req, resp) { // ... // your code to handle a request should be placed here... /// ... resp.end(data); });
A restructuring might look something like this:
'use strict'; const fs = require('fs'); var http = require('http'); var url = require('url'); function requestHandler(request, response) { var queryData = url.parse(request.url, true).query; var reqParam = queryData.soaclassification var data=[],i; console.log("Reading applications.json"); var rawdata = fs.readFileSync('applications.json', 'utf-8'); var applications = JSON.parse(rawdata); var dataValue; var dataobject = {}; // I'm not sure what's going on here.... // It seems like we could accomplish this much more simply // with a filter and map, but I'm not clear. for(i = 0; i < applications.length; i++) { dataobject = applications[i]; var dataobjectvalue = JSON.stringify(dataobject); dataValue = dataobject["soa-classification"]; if(dataValue === reqParam) { if(i===0) { data = dataobjectvalue; } else { data = data +','+ dataobjectvalue; } } } response.writeHead(200, {"Content-Type": "text/plain"}); response.end(JSON.stringify(data)); } console.log('node.js application starting...'); var svr = http.createServer(requestHandler); svr.listen(9000, function() { console.log('Node HTTP server is listening'); });
If you can send me the applications.json file, or an example of same, and also send me what you expect to get as a response, I could implement the requestHandler so that it does what you're imagining.
Hang on, I checked your other post and got an example of the applications.json file.
See attached for a working API Proxy.
You can deploy this to your org+env and invoke it, like so:
curl -i https://ORG-ENV.apigee.net/vidisha-1?soaclassification=service%20provider
You have several problems in that code.
var svr = http.createServer(function(req, resp) { // ... // your code to handle a request should be placed here... /// ... resp.end(data); });
A restructuring might look something like this:
'use strict'; const fs = require('fs'); var http = require('http'); var url = require('url'); function requestHandler(request, response) { var queryData = url.parse(request.url, true).query; var reqParam = queryData.soaclassification var data=[],i; console.log("Reading applications.json"); var rawdata = fs.readFileSync('applications.json', 'utf-8'); var applications = JSON.parse(rawdata); var dataValue; var dataobject = {}; // I'm not sure what's going on here.... // It seems like we could accomplish this much more simply // with a filter and map, but I'm not clear. for(i = 0; i < applications.length; i++) { dataobject = applications[i]; var dataobjectvalue = JSON.stringify(dataobject); dataValue = dataobject["soa-classification"]; if(dataValue === reqParam) { if(i===0) { data = dataobjectvalue; } else { data = data +','+ dataobjectvalue; } } } response.writeHead(200, {"Content-Type": "text/plain"}); response.end(JSON.stringify(data)); } console.log('node.js application starting...'); var svr = http.createServer(requestHandler); svr.listen(9000, function() { console.log('Node HTTP server is listening'); });
If you can send me the applications.json file, or an example of same, and also send me what you expect to get as a response, I could implement the requestHandler so that it does what you're imagining.
Hang on, I checked your other post and got an example of the applications.json file.
See attached for a working API Proxy.
You can deploy this to your org+env and invoke it, like so:
curl -i https://ORG-ENV.apigee.net/vidisha-1?soaclassification=service%20provider
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |