NodeJs Tutorials
- NodeJs
- Install NodeJs
- Why use NodeJs
- NodeJs Process Model
- create First Application
- Run NodeJs Application
- Node.js Console
- Node.Js Modules
- URL Modules
- node.js Callback
- Node.js Events
- Upload Files
- Upload single & Multiple Files
- NodeJs File System
- NodeJs Email
- Debugging NodeJs
- .ENV
- NodeJs Mysql
- Helpers
- encription and decription in nodeJs
- Query string
- Date & Time
- Express Js
- Template Engine with Express
- MVC Pattern in Node.Js
- list of NPM Module
- Middleware
- Body Parser
- Render
- Nodemon module
- Morgan module
- Flash Message in ExpressJs
- Session
- Session store in database
- Cookies
- Helmet
- Multer
- Router: How To Use Routing In Node.Js
- App.Js
- express.json() and express.urlencoded()
- REST APIs in NodeJs
- Gloabal Objects
- Submit Form Data
- How to get Post Data in Node.js
- How to Get URL Parameters in Node.js
- How to create Node Project
- How to Insert Form Data Into the MySql Table Using Node.js
- How to fetch Data from MySQL database table using Node.js
- CRUD Example
- Await and Async
- Promises
- Login Example
- Password Encription
- How to validate Form (Form Validation) in Node.Js?
- Registration & Login form usingn Node.Js & MySql?
- Forgot & Reset Password
- File Upload in Node.Js with ExpressJs
- Resize Image Before Upload using Multer Sharp
- Upload multiple file using node.js with multer module
- Upload file using node.js with multer module
- Client IP Address
- REST API Downloading File in NodeJs
- Export and Download CSV From MySQL Database
- CRUD REST API
- CRUD REST API Example 2
- Enable HTTPS using Node
- How to send EMAIL using NodeJs?
- Dynamic dependent dropdown using NodeJs?
- Autocomplete Search
- Get and Send Data From Ajax Request
- Get and Post Data using Ajax
- Passport Authentication
- Node Js Data type
- Node Js Error
- Node Js Array Function
- Node Js String Function
- Puppeter Module
What .env and How to use .env file in Node.Js application?
The .env file for handling environment variables. it is the most popular option in the Node.js.
we can create .env file in the application's root directory. That contains key/value pairs defining the project's required environment variables.
The .env library reads this.env file and appends it to process.env.
install dotenv package
File Name :
npm i dotenv --save
File Name :
require('dotenv').config()
File Name :
require('dotenv').config()
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})
1. Add .env to gitignore
File Name :
# Environment variables.
STATUS=production
#Development port
DEV_PORT=3000
#Production port
PROD_PORT=8000
#DB CONFIG
HOST=db.host
USER=root
PASSWORD=db.password
DB=db.name
DIALECT=mysql
File Name :
1. Commit the changes to your repository
git add .gitignore
git comit -m "adding .env.to .gitinore"
File Name :
NOTE
.env includes sensitive information, so it should not be pushed with your code/GIT repo. Please make sure!
This is how you can avoid .env to push in your repo using GIT.
Create a file called .gitignore in the root directory of your project, then open it, then write .env in it. Now git will avoid pushing your .env file
File Name :
We can attempt to parse an integer value from the PORT environment variable, or default to Port 5000 if the environment variable is not defined or contains something other than an integer:
const port = parseInt(process.env.PORT, 10) || 5000;
The name returned in the HTTP response can be found in the MYNAME environment variable or defaults to the string mahi:
const name = process.env.MYNAME || "mahi"
File Name :
http.createServer((request, response) => {
// ...
}).listen(port);
File Name :
File Name :