Nefarious Planetary Meddling


    0.0.5 • Public • Published


    npm install --save hapiest-cloudfront-url

    Basic usage

    I highly recommend you use this with node-config if possible though it's not a requirement.

    With node-config

    Include a config section that adheres to the following (assuming JSON format):

        "myCloudfrontDistribution": {
          "cloudfrontDomainName": "",
          "enabled": "true",
          "origins": [{
            "host": "",
            "path": "/public"
            "host": "",
            "path": "my/crazy/path"
            "host": "localhost",
            "port": 3000,
            "path": "localstorage/bucket/public"

    You then simply use the createFromNodeConfig function provided:

    const NodeConfig = require('config');
    const CfUrlServiceFactory = require('hapiest-cloudfront-url');
    const cfUrlService = CfUrlServiceFactory.createFromNodeConfig(NodeConfig, 'myCloudfrontDistribution');

    Without node-config

    const CfUrlServiceFactory = require('hapiest-cloudfront-url');
    const cfUrlService = CfUrlServiceFactory.create({
        cloudfrontDomainName: '',
        enabled: true,
        origins: [{
            host: '',
            path: '/public'
            host: '',
            path: 'my/crazy/path'
            host: 'localhost',
            port: 3000,
            path: 'localstorage/bucket/public'

    Converting an origin URL to Cloudfront URL

    When the originUrl provided the convertUrl function matches one of the URLs associated with the distribution, the return value is a URL relative to the Cloudfront domain.

    const originUrl = '';
    const cfUrl = cfUrlService.convertUrl(originUrl);
    const originUrl2 = '';
    const cfUrl2 = cfUrlService.convertUrl(originUrl2);
    const originUrl3 = 'http://localhost:3000/localstorage/bucket/public/images/thumb.jpg'
    const cfUrl3 = cfUrlService.convertUrl(originUrl3);

    If the provided URL does not match an origin, the provided URL is simply returned:

    const originalUrl = '';
    const cfUrl = cfUrlService.convertUrl(originUrl);
    // ''

    When enabled=false, convertUrl always returns the original URL though you can override that behavior by passing in a second parameter of true.


    npm i hapiest-cloudfront-url

    DownloadsWeekly Downloads






    Last publish


    • jonathanfulton
    • njgerner