This is a quick tutorial on using dotenv in JavaScript Development
When building apps, we continually need access to databases. To access those databases, we need to be authenticated to be authorized to access the resources stored.
In Web Development/app building, especially when storing code in a publicly accessible repository like GitHub, we should be conscious of our credentials. The way to get around storing your code on without credentials on GitHub is by using dotenv.
dotenv is a JavaScript package that enables users to and access credentials safely.
In your server.js file, add the following code
const express = require(“express”);
const app = express();
const dotenv = require('dotenv')
...
dotenv.config()
...
const password = process.env.[variable_name]
...
app.listen(3000, () => console.log(“server has started at port 3000”));
In the home directory, create a .env file. In the .env file create the variable name referenced in the using the following format.
variable_name="The value of the variable"
In this example, the value of the const password will be the string "The value of the variable"
The contents of the variable can be modified to match any string variables such as URIs, usernames, password, or any other sensitive information to be passed into a Web Application.
Comments
Post a Comment