开发者

how to print out JSON output

I am trying to call API from the website and print out the JSON result. but I cannot see the result. Any thought to figure out this problem. Thank you.

 var http = require('http');
 var data_info="";

  http.createServer(function (req, res) {
  sendJsontoAlchemy(outputMode());
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('end\n');
 }).listen(1111)
 console.log('Server is on');

 function sendJsontoAlchemy()
 {
     requestNumber = JSONRequest.post(
     "http://access.alchemyapi.com/calls/text/TextGetCategory", 
     {
                 apikey: "aaaaaa",
               text : "Skateboard Mens Trousers t Shirt Wooden Fence",
              ou开发者_运维技巧tputMode : json, 
              jsonp:outputMode//call back function.
     }, 
    function (requestNumber, value, exception) {
        if (value) {
            processResponse(value);
        } else {
            processError(exception);
        }
    }); 
}

function outputMode(response)
{
    console.log("the result is =>");    
    console.log(JSON.stringify(response));
}


I'd suggest using the very useful module request for such things.

Example Code:

var request = require('request');

request({
    method: 'POST',
    uri: 'http://access.alchemyapi.com/calls/text/TextGetCategory',
    body: 'apikey=1337abcd'+
            '&text=Skateboard Mens Trousers t Shirt Wooden Fence' +
            '&outputMode=json',
}, function(err, res, body){
    result = JSON.parse(body);
    console.log('Category: ', result.language);
    console.log('Category: ', result.category);
});

If you need to output the thing from a server you can do a request({opts}).pipe(response) inside a server instead of specifying a callback.

var http = require('http');
var request = require('request');

http.createServer(function(req, res){
    request({
        method: 'POST',
        uri: 'http://access.alchemyapi.com/calls/text/TextGetCategory',
        body: 'apikey=1d416bcdde7cf8a360fad31701ac2c2ba57bc75f'+
                '&text=Skateboard Mens Trousers t Shirt Wooden Fence' +
                '&outputMode=json',
    }).pipe(res);
}).listen(1111);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜