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]

3
  • 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. Commented Nov 4, 2012 at 3: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 Commented Nov 4, 2012 at 4:02
  • Have you checked this related question... stackoverflow.com/questions/5892569/… Commented Nov 4, 2012 at 4:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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, { .....
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.
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.