npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-offline

v1.1.0

Published

Allows an express app be placed into offline mode and automagically serve responses from disk.

Downloads

5

Readme

express-offline

Linked In Twitter Follow

Allows an express app be placed into offline mode and automagically serve responses from disk.

Installation

npm install --save express-offline

Usage

In the example below, the /microdata route extracts microdata from any website and returns it as a JSON object. So, if we run the example and request localhost:8080/microdata?url=http://mammothworkwear.com/dickies-proban-coverall-p562.htm it will return a JSON object containing product information:

{
    ...
    {
        "id": "14ef1e7a452ceca27c8124b3c26f4b74",
        "name": "http://schema.org/Product",
        "properties": {
            "name": "Dickies Proban Coverall",
            "manufacturer": "Dickies",
            "brand": "Dickies",
            "mpn model": "FR4869",
            "image": "http://mammothworkwear.com/dickies-proban-coverall-w385h385q94i6368.jpg",
            "description": "If your workplace really cranks up the heat - literally as well as figuratively - then the Dickies Proban Coverall is for you. Constructed from special flame-retardant fabrics to comply with safety standards EN531 and EN470, this boiler suit offers high quality and reliable performance in hazardous areas. If you can expect a tripartite attack from fire, sparks and molten materials all before lunchtime, you'll need this kind of self-extinguishing clothing. Made to limit burn injuries and protect you against extreme heat, the overalls are ideal for those who work in the welding trade. As well as sporting a proban finish, the garment comes with a concealed stud front and cuffs with stud closure. There's also a studded neck closure, to ensure no sections of clothing come unhoused and dangle over flames and suchlike. Constructed from 330g of 100% cotton drill, this one comes with reinforced stress points and a pair of patch pockets at the chest. There are also further pockets in the seat and at the sides. If you can't stand the heat, don't get out of the workplace - get into the Dickies Proban Flame Retardant Overalls.",
            "offers": "1 to 445.47"
        }
    }
    ...
}

We probably dont want to be hitting the workwear site for every request during development, so we add the express-offline middleware and run our app with the environment variable OFFLINE=true.

Future requests will now return the contents of ./offline/development/get/microdata.json.

var express = require('express');
var getMicrodata = require('node-microdata-scraper');   // extract microdata from a website
var app = express();                                    // create an instance of express
var offline = require('express-offline');                // load express-offline middleware module

// add express-offline middleware to intercept requests (must be added before all other routes)
app.use(offline());

// demo express route to extract microdata from a webpage passed as a query-string
app.get('/microdata', function(req, res) {

    var url = req.query.url;

    // extract microdata from URL and return to browser
    getMicrodata.parseUrl(url, function(err, data) {

        var json = JSON.parse(data);

        res.json(json);
    });
});

app.listen(8080);

Mapping requests to files

By default express-offline will look for an offline folder in the root of your application. Path's are them mapped using the following formula:

./offline/ + NODE_ENV + / + HTTP_METHOD + / + REQUEST_PATH.ACCEPT_HEADER (all in lowercase!)

So the following GET request to your application running in development mode would return ./offline/development/get/microdata.json

GET /microdata?url=http://mammothworkwear.com/dickies-proban-coverall-p562.htm HTTP/1.0
Accept: application/json

The following POST request to your application running in production mode would return ./offline/production/post/microdata.html

POST /microdata HTTP/1.0
Accept: text/html

Example

This project includes a working example. You can run in online mode using:

node ./node-modules/express-offline/example.js

And in offline mode using:

OFFLINE=true node ./node-modules/express-offline/example.js

Contribute

The following tasks need doing, so please feel free to contribute:

  • Add unit tests
  • Clean up documentation

License

Licensed under ISC License © John Doherty