UNPKG

bquery

Version:

bquery is a useful node module to fetch web page, which use css selector to fetch and structure this html page content.

114 lines (79 loc) 3.34 kB
--- category: reference heading: 'Noodle as node module' --- **Note:** Since noodle's internal cache uses an interval this will keep the related node process running indefinately. Be sure to run `noodle.stopCache()` in your code when you're finished with noodle. ## Methods ### noodle.query The main entry point to noodle's functionality is the `query` method. This method accepts a query or an array of queries as its only parameter and returns a [promise](https://github.com/kriskowal/q). var noodle = require('noodle'); noodle.query(queries).then(function (results) { console.log(results); }); The makeup of query(s) is analagous to using noodle as a web service (as [stated above](http://noodlejs.com/reference/#query-syntax)). The exception being that you supply a proper object and not JSON. ### noodle.fetch This method returns a [promises](https://github.com/kriskowal/q). Which upon resolutions hands over the requested web document. noodle.fetch(url).then(function (page) { console.log(page); }); ### noodle.html.select For applying one query to a html string and retrieving the results. noodle.html.select(html, {selector: 'title', extract: 'innerHTML'}) .then(function (result) { console.log(result); }); ### noodle.json.select For applying one query to a parsed JSON representation (object). var parsed = JSON.parse(json); noodle.html.select(parsed, {selector: '.name'}) .then(function (result) { console.log(result); }); ## noodle.feed.select Normalises an RSS, ATOM or RDF string with [node-feedparser](https://github.com/danmactough/node-feedparser) then proxies that normalised object to `noodle.json.select`. ### noodle.xml.select Proxies to `noodle.json.select`. ### noodle events noodle's `noodle.events` namespace allows one to listen for emitted cache related events. Noodle inherits from node's [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter). // Called when a page is cached noodle.events.on('cache/page', function (obj) { //obj is the page cache object detailing the page, its headers //and when it was first cached }); // Called when a result is cached noodle.events.on('cache/result', function (obj) { //obj is the result cache object detailing the result and when //it was first cached }); // Called when the cache is purged noodle.events.on('cache/purge', function (arg1, arg2) { //arg1 is a javascript date representing when the cache was purged //arg2 is the time in milliseconds until the next cache purge }); // Called when a cached item has expired from the cache noodle.events.on('cache/expire', function (obj) { //obj is the cache item }); ### Configuration Configuration is possible programmatically via `noodle.configure(obj)`. This accepts a conig object which can be partly or fully representing the config options. This object is applied over the existing config found in the `config.json`. Example for change just two settings: var noodle = require('noodle'); // Do not display messages to the terminal and set // the default document type to json noodle.configure({ debug: false, defaultDocumentType: "json" });