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

isomorphic-config

v0.2.3

Published

A configuration manager for isomorphic applications

Downloads

529

Readme

Isomorphic Config

A configuration manager for isomorphic applications

installation

npm install --save isomorphic-config

Usage

First of all, you need to create a "config" directory in the root of your project and add a "default.json" file in it with the settings required by your app.

From server

Just import and use it, isomorphic-config will make use of server side capabilities to read the configuration file ("/config/default.json") and retrieve the configurations in it.

const isomorphicConfig = require("isomorphic-config");
const config = isomorphicConfig.server;
const express =  require("express");

let server = express();
server.listen(config.port, (err) => {
    if (err) {
        throw new Error(err);
    } else {
        console.info('Listening at http://localhost: ', config.port);
    }
});

From client

In order to use the config in a client app, you would need to expose it in a global variable ("CONFIG") from the server initial render, for later use in the app (see the "Hello" example).

server initial render (server.js)
const isomorphicConfig = require("isomorphic-config");

const express = require('express');
const server = express();
const hello = require("./Hello.js");

// You would be wise to only expose to the client non-sensitive configuration.
// It's a good idea to keep all client configurations in a "client" key and only expose that:
const clientConfig = {client: isomorphicConfig.client};
server.get('/', function (req, res) {
    res.send(
        `<!DOCTYPE html>
        <html lang="en">
           <head>
                <title>Hello Isomorphic Config</title>
                <script charSet="UTF-8">var CONFIG=${JSON.stringify(clientConfig)}</script>
           </head>
           <body>
               ${hello}
               <script>alert(CONFIG.client.greeting);</script>
           </body>
        </html>`
    );
});
server.listen(isomorphicConfig.server.port, function () {
    console.log(`Example app listening on port ${isomorphicConfig.server.port}!`);
});
Hello component (Hello.js)
const isomorphicConfig = require("isomorphic-config");
const config = isomorphicConfig.client;
const hello = `<div class="greeting">${config.greeting}</div>`
module.exports = hello;

Environment Variables

Inspired by the config module, you can define environment variables to override specific configurations. If you're running on a server, isomorphic-config will check for the existance of a given environment variable and replace the config associated to it with it's value.

To enable custom environment variables, create a configuration file, custom-environment-variables.json (and place it in the "config") directory mapping the environment variable names into your configuration structure. For example:

{
  "server": {
    "port": "PORT"
  },
  "client": {
    "greeting": "GREETING"
  }
}

...would cause isomorphic-config to check for the environment variables PORT and GREETING. If they exist, they would override the values for server.port, and client.greeting in your configuration.

Why use it?

to centralize the way configurations are requested throughout your isomorphic application. The config module works only on server side because it uses file system to read a config file. Isomorphic Config does the same but also attemps to read the configuration from a client Global variable. Also, unlike config, it only supports json format, so code is more straight-foward and has less dependencies.