mu.js

1.1.2 • Public • Published

µ.js is the new $

This package is top secret and should not be used by anybody under any circumstances.

  • Simplified HTTP request client. request
  • Tiny, fast, and elegant implementation of core jQuery designed specifically for the server. cheerio

Examples

/* Initialize jQuery on remote document */
let µ = require('mu.js')
µ('http://example.com/').then($ => {
    // $ = cheerio (jQuery)
    console.log(
        $('body').html()
    )
})
/* For options, use object passed to request module */
µ({
    url: 'http://example.com', 
    timeout: 5000, 
    strictSSL: true // require valid SSL
}).then($ => {
    console.log(
        $('title').text()
    )
})
/* From local file */
µ.fromFile('page.html').then($ => {
    console.log(
        $('title').text()
    )
})
/* From local file synchronously */
let $ = µ.fromFileSync('/var/www/index.html')
$('h2').each(function() { /* make text red in every <h2> */
    $(this).css('color', 'red')
})
/* 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> */
/* Full document outerHTML*/
$.html()
/* Initialize jQuery on remote document synchronously */
const $ = µ.sync('http://reddit.com')
$('title').text() // reddit: the front page of the internet
/* 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).

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
})

Readme

Keywords

Package Sidebar

Install

npm i mu.js

Weekly Downloads

4

Version

1.1.2

License

ISC

Unpacked Size

6.92 kB

Total Files

5

Last publish

Collaborators

  • jochemstoel