UNPKG

mu.js

Version:

µ is the new $

115 lines (99 loc) 3.48 kB
# µ.js <small>is the new $</small> This package is top secret and should not be used by anybody under any circumstances. * Simplified HTTP request client. [request](https://www.npmjs.com/package/request) * Tiny, fast, and elegant implementation of core jQuery designed specifically for the server. [cheerio](https://www.npmjs.com/package/cheerio) ## Examples ```js /* Initialize jQuery on remote document */ let µ = require('mu.js') µ('http://example.com/').then($ => { // $ = cheerio (jQuery) console.log( $('body').html() ) }) ``` ```js /* For options, use object passed to request module */ µ({ url: 'http://example.com', timeout: 5000, strictSSL: true // require valid SSL }).then($ => { console.log( $('title').text() ) }) ``` ```js /* From local file */ µ.fromFile('page.html').then($ => { console.log( $('title').text() ) }) ``` ```js /* From local file synchronously */ let $ = µ.fromFileSync('/var/www/index.html') $('h2').each(function() { /* make text red in every <h2> */ $(this).css('color', 'red') }) ``` ```js /* From a string */ let $ = µ.fromString('<div><h2>Title of a document</h2><p>Description of a document.</p></div>') // does not even require <html></html> $('div').html() /* <h2>Title of a document</h2><p>Description of a document.</p> */ ``` ```js /* Full document outerHTML*/ $.html() ``` ```js /* Initialize jQuery on remote document synchronously */ const $ = µ.sync('http://reddit.com') $('title').text() // reddit: the front page of the internet ``` ```js /* Fetch worldnews headlines from Reddit */ µ('https://www.reddit.com/r/worldnews/').then($ => { $('p[class="title"]').each(function() { let a = $('a', $(this)) // every <a> in this <p> console.log({ title: a.text(), url: a.attr('href') }) }) }) /* { "title": "Elon Musk's SpaceX Falcon 9 rocket blasts off to deliver human sperm to International Space Station for NASA testsstandard.co.uk", "url": "https://www.standard.co.uk/news/world/spacex-falcon-9-elon-musks-powerful-rocket-blasts-off-to-deliver-human-sperm-to-international-space-a3804171.html" } { "title": "Russia threatens sanctions over Latvian language in Latvian schoolsbbc.com", "url": "http://www.bbc.com/news/world-europe-43626368" } { "title": "Robert Mueller’s Russia probe reportedly rolled a witness, George Nader, who confirmed that Blackwater founder Erik Prince did indeed attempt to set up a secret backchannel between Trump and Russia in January 2017uproxx.com", "url": "https://uproxx.com/news/russian-met-erik-prince-backchannel-link-putin/" } ... more */ ``` #### Update: 30 october 2019 Added additional method _sync_ for synchronous initialization. See example. #### Update: 1 april 2018 Now includes 3 additional methods: _fromFile_, _fromFileSync_ and _fromString_. #### Update: 4 september 2018 Resolved instance of jQuery (cheerio) now includes reference to HTTP _response_, which in turn includes a reference to the HTTP _request_. This can be used for stuff like reading header information (Content-type, cache-control, ...) and detect redirect(s). ```js let url = 'http://example.com' µ(url).then($ => { console.log($.response.headers) let destination = $.response.request.uri.href if(destination != url) console.log(`we were redirected from ${url} to ${destination}`) // the rest of your $ code }) ```