In this lesson, we will learn how to move constants to a separate file.
npm i dotenv
PORT=3000
const dotenv = require('dotenv');
dotenv.config()
const port = process.env.PORT;
console.log("PORT", port);
nodemon app.js
D:\restapi-lab\project-api>nodemon app.js
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
Server is listening on port 3000!
PORT=5000
D:\restapi-lab\project-api>nodemon app.js
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
Server is listening on port 5000!
git status
git app.js
git commit -m "Refactoring - Added PORT in environment variable"