Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/launchpadlab/expose-env-middleware
Expose env variables to client-side code via express
https://github.com/launchpadlab/expose-env-middleware
Last synced: 3 days ago
JSON representation
Expose env variables to client-side code via express
- Host: GitHub
- URL: https://github.com/launchpadlab/expose-env-middleware
- Owner: LaunchPadLab
- Created: 2019-04-24T00:01:18.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-14T13:15:12.000Z (over 4 years ago)
- Last Synced: 2024-10-14T01:12:26.712Z (25 days ago)
- Language: JavaScript
- Homepage:
- Size: 173 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# expose-env-middleware
Express middleware to dynamically expose environment variables to client-side code via `window.process.env`:
```js
// server.jsconst app = require('express')()
const exposeEnvMiddleware = require('expose-env-middleware')// Pass it a function that returns env vars
const getEnv = () => ({ FOO: 'bar' })
app.get('/env', exposeEnvMiddleware(getEnv))
app.listen(...)
```
```htmlconsole.log(process.env.FOO) // -> 'bar'
```This setup allows env vars to reload each time index.html is fetched.
## API
* `middleware(getEnv[, options])`
* `getEnv`: A function that returns an object of env variables.
* `options.template` (optional, default=`defaultTemplate`) Template function for serializing the env object into a file. Default is shown below:```js
function defaultTemplate (env) {
return `
window.process = {
env: ${ JSON.stringify(env) }
}
`
}
```