Code Monkey home page Code Monkey logo

nodesi's Introduction

Build Status Coverage Status Dependency status

What is this?

It's a subset of Edge Side Include standard implemented with promise-based interface.

Features

  • Support for esi:include tags
  • Out of the box Express support
  • Custom logging
  • Lots of good stuff like caching, retires, request collapsing and such provided by Good Guy HTTP

...and more, take a look at test cases for complete list.

Installation

npm install nodesi

Usage

Basic:

    var ESI = require('nodesi');

    var esi = new ESI();
    esi.process('<esi:include src="http://full-resource-path/stuff.html" />').then(function(result) {
        // result is a fetched html
    });

As Express middleware:

    var esiMiddleware = require('nodesi').middleware;
    var app = require('express')();

    // inject the middleware before your route handlers
    app.use(esiMiddleware());

All the ESI constructor options described below are also applicable for middleware function. Just pass them like that: esiMiddleWare({baseUrl: ...});

If you'd like to pass options like headers to ESI middleware, use req.esiOptions object:

...
    app.use(esiMiddleware());

    app.get('/example', function(req, res) {
        req.esiOptions = {
            headers: {
                'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='
            }
        };
        res.render('example');
    });

With base URL for relative paths:

    var ESI = require('nodesi');

    var esi = new ESI({
        baseUrl: 'http://full-resource-path'
    });
    esi.process('<esi:include src="/stuff.html" />').then(function(result) {
        // result is a fetched html
    });

With headers:

    var ESI = require('nodesi');

    var esi = new ESI({
        baseUrl: 'http://full-resource-path'
    });
    esi.process('<esi:include src="/stuff.html" />', {
        headers: {
            'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='
        }
    }).then(function(result) {
        // result is a fetched html
    });

Error handling

You can provide onError callback to a ESI constructor. It will recieve two arguments: source URL and error object.

It should return a string that will be put in place of errorous content.

Example

    var esi = new ESI({
        onError: function(src, error) {
            if(error.statusCode === 404) {
                return 'Not found';
            }
            return '';
        }
    });

Logging

It's a common anti-pattern that libraries write to stdout w/o users permission.

We want to be nice so you can provide your own logging output with logTo configuration option.

It's expected to be an object with "write" method on it that accepts a single string.

Examples

Logging to a custom object

    var esi = new ESI({
        logTo: {
            write: function(log) {
                // do some stuff with log string here
            }
        }
    });

Logging to a standard output (same as console.log):

    var esi = new ESI({
        logTo: process.stdout
    });

Logging to a file (possible, but please don't do that):

    var logFile = require('fs').createWriteStream('./log.txt');
    var esi = new ESI({
        logTo: logFile
    });

Performance testing

You can run performance tests with npm run perf [args]

This tool assumes you have Siege installed and added to your Path variable.

[args] are list of arguments that will be passed to Siege.

nodesi's People

Contributors

kjarmicki avatar kwasniew avatar gregersrygg avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.