Search code examples
javascriptjsonnode.jsx-ray

X-Ray scraping and present json in the server browser


I'm using x-ray to scrape a website, however i can't seem to show the correct JSON output in the browser. It works fine, when i write a new json doc like write('result.json') however now when i try to send it to the browser. i'm at the moment using express as the web framework.

This below creates a new result.json file and show the correct json output (urls on dribbble.com). However is not showing it in the browser as i want?

app.get('/api/standings', function(req, res, next){


    x('http://www.dribbble.com', 'a', [{
    url: '@href',
    }]).write()
'results.json'


});

what i've tried

app.get('/api/standings', function(req, res, next){

    res.send(x('http://www.dribbble.com', 'a', [{
        url: '@href',
    }]).write());



});

weird wrong output

{
  "_readableState": {
    "objectMode": false,
    "highWaterMark": 16384,
    "buffer": [

    ],
    "length": 0,
    "pipes": null,
    "pipesCount": 0,
    "flowing": null,
    "ended": false,
    "endEmitted": false,
    "reading": false,
    "sync": true,
    "needReadable": false,
    "emittedReadable": false,
    "readableListening": false,
    "defaultEncoding": "utf8",
    "ranOut": false,
    "awaitDrain": 0,
    "readingMore": false,
    "decoder": null,
    "encoding": null
  },
  "readable": true,
  "domain": null,
  "_events": {

  },
  "_eventsCount": 0
}

Solution

  • The .write will return you a enstore stream. You have pipe it to your node GET response to make it work.

    app.get('/api/standings', function(req, res, next){
    
        x('http://www.dribbble.com', 'a', [{
        url: '@href',
        }]).write().pipe(res);
    
    });