It's been a while since I've written anything, so now I'm writing a crappy post. Also useful for some of you doing NodeJS.
- Original post: Use env (process.env) effectively
- Next post: How do I write the @ltv/env package
overview
process.env is a global variable in NodeJS that is set automatically when the nodejs app starts. It can be visualized like this: When we set the environment variable for the operating system to export NODE_PORT=3000, we will get that information in process.env, it will be: process.env.NODE_PORT
You are watching: What is Env
Is this environment variable important?
It's important to throw the pot out. Let me give you an example of a jump. Suppose my NodeJS app has a DB connection and its information is like this:
dastabase_host=127.0.0.1 database_user=root database_port=3306 database_pass=SecurePass
When used locally, you can define whatever you want, hard set is fine. But when you guys bring deploy to server / production / dev / or something else, what happens to it? If the set is hard, you will have to edit the information in the code. Each time, it will have to be fixed. It bothers vcl.
If there are only 4 of the above, it should not be a problem but usually it will be a lot more than that, maybe a few dozen.
Therefore, the information that can / needs to be changed when changing the running environment should (must) be left in the environment variable.
How to use it?
Basically, before starting the NodeJS app, you must set ENV for the operating system. But it's annoying, if there are a few dozen different envs that dads bring to set before starting, it's really sick. So no one does. Usually it uses something to set automatically. But in this case, I should not set it to the environment variable of the OS because if I dev several different apps, it will conflict env. The best way is to set the process.env of each app separately.
Somewhere it looks like this:
process.env.DATABASE_PORT=3000
With the NodeJS app, we have a pretty famous package to set the env for the process.env variable, which is dotenv. And my job is to bring all env into one file. For example .env / local.env for example.
Reference: A Tutorial on EDA and Feature Engineering
For example: local.env
DATABASE_HOST=127.0.0.1 DATABASE_PORT=3306 DATABASE_USER=root DATABASE_PASS=SecurePass
Then in the code I load like this (app.js / index.js):
require('dotenv').config({ path: 'local.env' })
After that, the global process.env variable will have all the envs that I have defined in the local.env file and can be used anywhere in the NodeJS app.
VD:
const connection = new Connection({ host: process.env.DATABASE_HOST, port: process.env.DATABASE_PORT, user: process.env.DATABASE_USER, pass: process.env.DATABASE_PASS });
Use it so it's delicious
The story above must have been understood by comrades. The problem now is how to use it properly.
Back to the story above, if you have an env file, it's okay, but if you don't have it, it's broken. The process.env pile will have nothing. So now if there is no env, what will be done?
The obvious thing is to check null and then set default env and nothing more.
const connection = new Connection({ host: process.env.DATABASE_HOST || 'localhost', port: process.env.DATABASE_PORT || 3306, user: process.env.DATABASE_USER || 'root', pass: process.env. DATABASE_PASS || 'noPASSon' });
Uh. Seems to have solved the problem. But if we go a little deeper, all envs have a value type of string. That means the guy process.env.DATABASE_PORT will be: '3306', not 3306. What I want, it should be 3306.
Similarly, the worst case scenario could go like this:
Related Content: Homogenizer
You have:
DATABASE_SSL=false
When used it is:
const connection = new Connection({ …otherConfigs, ssl: process.env.DATABASE_SSL || true });
Attention dads. process.env.DATABASE_SSL is the string 'false' And the string other than empty is undefined and non-null means ssl=true.
It's dead. You guys are trying to set ssl=false. It's more complicated, isn't it?
So that means one more thing has arisen. Parents are required to cast the type from string to the exact type they want to use. In the end, there are 2 things to do:
So how to grab the skirt to make it neat and delicious?
For brevity, comrades install this guy:
yarn add @ltv/env # npm i -S @ltv/env
And just use:
const env = require('@ltv/env') // Or can use with import import env from '@ltv/env' const dbPort = env.int('DATABASE_PORT', 3000) # or string const dbHost = env(' DATABASE_HOST', 'localhost') # or bool const useSSL = env.bool('DATABASE_SSL', false) # First arg is VARIABLE NAME # Second arg is DEFAULT VALUE
Using the above example, it looks like this:
const connection = new Connection({ host: env('DATABASE_HOST', 'localhost'), port: env.int('DATABASE_PORT', 3306), user: env('DATABASE_USER', 'root'), pass: env( 'DATABASE_PASS', 'noPASSon)', ssl: env.bool('DATABASE_SSL') });
In the next article we will learn how to write this lib later. Some of you are familiar with NodeJS, you can go to:
Reference: Hydroxyl
Post a Comment
Post a Comment