Question

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]

No correct solution

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top