0

I want to return a Json object in 200 OK.

I am getting the data from my nosql.

I know the how to fill the structure. (that is in the json body the values to keep)

Here Name, Class and Address are fixed:

Name= entity[4]
Class= entityclass[4]
Address = entityaddrss[4]

... enity..will be coming from nosql.

how can I construct and send a 200Ok with json body.

response.end({})

can you plesae let me what I should write in end.I have all the required data: Name= entity[4] Class= entityclass[4] Address = entityaddrss[4]

The Learner
  • 3,867
  • 14
  • 40
  • 50
  • this needs a lot more detail if you want a good answer. What database are you using, exactly? what library are you using to interact with the database? what kind of data structure is that in your example - 3 variables, an object, a custom object? what node server are you using - connect or express? etc. etc. – Jeff Escalante Nov 04 '12 at 03:59
  • Thanks for the response. I am using Azure . here my question is more how to send the data to the user in 200 OK. I am able to get the values. I know how to assign and make the json body as shown above. all I am looking is given the values how I can construct and send in 200 OK. my DB might change to mongo..so dont want to discuss about DB..assuming that eveerything is available want to know how to send jason body in 200 ok – The Learner Nov 04 '12 at 04:02
  • Have you checked this related question... http://stackoverflow.com/questions/5892569/responding-a-json-object-in-nodejs – almypal Nov 04 '12 at 04:03

1 Answers1

0

Ok now that you added a few details in your first comment (you should edit the question and add these), I think I can answer what you are after.

var http = require('http')
var port = process.env.PORT || 1337;

var get_nosql_data = function(callback){
  // do whatever it is here you need to get the data back
  // and format it into a json object. pseudocode follows.
  // error handling is also needed here, but not relevant to the answer
  var db = require('db');
  db.getData('some query', function(res){
    var data = { name: res.entity[4], class: res.entityclass[4], address: res.entityaddrss[4] };
    callback(data);
  });
}

http.createServer(function(req, res) {
  get_nosql_data(function(data){
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify(data));
  });
}).listen(port);

As long as you sub in the database values for the placeholder strings I have there, you should be up and running. This also should align with what azure already has set up for you.

Related, I would highly recommend checking out express - this will make your life a lot easier writing node web apps.

Jeff Escalante
  • 3,137
  • 1
  • 21
  • 30
  • I am calling a different function here which will contact the nomysql.so this as is will not work. so, function is called and I have the array before I start the res.writeHead(200, { ..... – The Learner Nov 04 '12 at 04:22
  • I see nothing indicating that the `end` method of an `http.serverResponse` object will automatically serialize a passed object into JSON; the object should be passed into `JSON.stringify()` first. – ebohlman Nov 04 '12 at 09:49
  • Thanks @ebohlman can you let me know how to pass the paramsters as shown in the question. I did not understand even after looking some examples – The Learner Nov 04 '12 at 18:25
  • good call @ebohlman , updated the answer. The Learner, I still need more details. This will work, but I can't help unless you provide more code, like the function that gets the nosql response. I updated my answer with my guess as to how it would happen. – Jeff Escalante Nov 05 '12 at 20:38