Libraries - GradedJestRisk/js-training GitHub Wiki
List:
- network:
- make http calls: axios
- git hooks:
- handle HTTP exception: boom.js
- handle separate config files:
- dependency injection: awilix
- test
-
lint
- eslint
- standard
- lint-staged
- prettier
- editorconfig
- security
- [eslint-security]
- routing
- development
- watch for changes: nodemon
- environment
- environment variable:
- dotenv
- dotenv-cli
- environment variable:
- string
- count: letter-count:
node -e "console.dir(require('letter-count').countFromFile('./package.json'));"
- count: letter-count:
- logging
12factor recommend to store configuration in the environment, not in the codebase. One way to do so is to use environment variables to pass configuration. Your application should not load them, but just read them.
Tradeoffs for local development include .env file (see below). Still, they use local-scope variable so mitigate risks. If you tried to handle configuration with npm script, you may try globals with npm, but it wouldn't work.
Why ?
- in Linux, you can't mutate globals globally, as they are not shared, merely copied.
- npm create a subshell each time it executes
scripts: { "setup": "export FOO=bar"}
2 solutions:
- do not follow 12-factor and use node .env libraries (see below)
- use docker and OS-level tools like direnv
CI/CD platforms (eg. Heroku) load configuration (eg. port):
- from files named .env
- to environment variables
- exclude from versioning: add to
.gitgnore
file
.env* !.env.example
- create a
.env
file
touch .env
- set variables
FOO=bar BAR=3
You can document what you expect (commit these files)
-
.env.example
-
sample.env
List:
- install:
npm install dotenv
- create local file:
touch .env
- add a key :
API_PORT=3000
- import
require('dotenv').config()
- use it
const port = process.env.API_PORT
You can extract dotenv import by putting it in the command line.
node -r dotenv/config index.js
dotenv-cli expose variables in command-line scripts. Useful for CLI, eg mocha
List:
- no clean way to exclude console.log mutation yet (PR here)
const nock = require('nock') const scope = nock('https://api.github.com') .get('/repos/atom/atom/license') .reply(200, { license: { key: 'mit', name: 'MIT License', spdx_id: 'MIT', url: 'https://api.github.com/licenses/mit', node_id: 'MDc6TGljZW5zZTEz' } })
// arrange const GoogleAPICallNock = nock('http://google.com') .get('/') .reply(200); // act callAPI(); // assert assert.strictEqual(GoogleAPICallNock.isDone() , true);
API
DEBUG=nock.* npm test
Install npm install collections
Choose the collection here