Package Exports
- isomorphic-config
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (isomorphic-config) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Isomorphic Config
A configuration manager for isomorphic applications
installation
npm install --save isomorphic-configUsage
From server
Just import and use it, isomorphic-config will make use of server side capabilities to read a configuration file 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. Isomorphic-config will detect it and retrieve it (see the "Hello" example)
// server initial render (server.js)
const isomorphicConfig = require("isomorphic-config");
const clientConfig = isomorphicConfig.client;
const serverConfig = isomorphicConfig.server;
const express = require('express');
const server = express();
const hello = require("./Hello.js");
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}
</body>
</html>`
);
});
server.listen(serverConfig.port, function () {
console.log(`Example app listening on port ${serverConfig.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 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.