An warware: dotenv

A cikin duniyar shirye-shirye, sarrafa sauye-sauyen yanayi na iya zama da wahala, musamman ga manyan aikace-aikace tare da daidaitawa da yawa. Koyaya, akwai fakiti guda ษ—aya mai kyau wanda zai iya sa wannan aikin ya zama iska; Dotenv. Dotenv, da farko da aka yi amfani da shi a cikin Node.js, yana bawa masu haษ“akawa damar raba bayanan sirri ko daidaita masu canji daga lambar su kuma yana sa ya zama mai amfani sosai don sarrafa waษ—annan masu canji a cikin mahallin ci gaba daban-daban.

Dotenv zuwa ga Ceto

Sabanin al'ada na gama gari inda masu haษ“akawa ke shigar da jeri kai tsaye cikin lambar ko amfani da abin 'process.env' na kumburi, dotenv yana ba da ingantaccen tsari. Yana ba ku damar adana bayanan sirrinku a cikin fayil ษ—in '.env' daban, yana sa ya fi aminci da sauฦ™in sarrafawa.

Ainihin aiwatarwa yana farawa ta hanyar shigar da dotenv ta amfani da npm shigar dotenv umarni. Wannan aikin yana ฦ™ara dotenv zuwa ฦ™irar ฦ™irar ku, yana ba ku damar amfani da shi a cikin aikace-aikacen ku.

//Importing the package
import dotenv from 'dotenv';
//Configure dotenv
dotenv.config();

Da zarar an saita dotenv, zaku iya samun dama ga masu canjin yanayi ta amfani da abun 'process.env'. Ana adana masu canjin yanayi a cikin fayil ษ—in '.env' da ke cikin tushen tushen aikace-aikacen ku. Wannan fayil ษ—in yana da tsari mai tsari na 'KEY=VALUE'.

Tsokaci Kan Abubuwan Da Yawaita

Samun bayanan daidaitawa daban yana hana zubar da mahimman bayanai ba da gangan ba kuma yana rage rikitaccen tsarin lambar ku. Yana ba ku fa'ida ta samarwa mafi girman sassauci, amintacce handling na bayanai masu mahimmanci, da kuma ikon ci gaba da daidaitawa daban-daban a wurare daban-daban.

A cikin yanayin turawa, kawai ta hanyar canza fayil ษ—in '.env', zaku iya tabbatar da saituna daban-daban don haษ“akawa, tsarawa, da wuraren samarwa ba tare da shigar da ainihin codebase ba.

//Access variable from .env file
console.log(`Server running on ${process.env.PORT}`);

Manyan Laburaren da Ayyuka

Hakanan yana taimakawa sanin dakunan karatu kamar dotenv-lafiya da kuma dotenv-extenv. Ba wai kawai suna ba da aikin dotenv ba, sun zo tare da ฦ™arin fasali kuma waษ—anda zasu iya tabbatar da amfani dangane da bukatun ku. Misali, dotenv-safe yana tabbatar da cewa an saita duk masu canjin yanayi kafin fara aikace-aikacen ku.

Sarrafa masu canjin yanayi a aikace-aikacen Node.js yana buฦ™atar dabarar hanya. Wannan ba kawai yana tabbatar da tsaro da sassauci ba, har ma yana kiyaye lambar tsabta da tsari. Tare da mafita kamar dotenv, fayilolin .env za a iya amfani da su cikin aminci don adana duk saitunan da suka dace. Kasancewar irin waษ—annan fakitin yana haษ“aka sassauฦ™a da ฦ™arfi na aikace-aikacen ku, yana mai da shi mafi dacewa ga wurare daban-daban. Happy codeing!

Shafi posts:

Leave a Comment