InterviewSolution
| 1. |
How to config properties in Express.js? |
|
Answer» It is always a bad idea in production projects to have configuration data in the server.js file or the main file. In production projects for Express.js, we configure the properties in two ways. They are -
We will look into the config.js method to store the configuration properties. First, we will have our Node runtime installed and created using npm init -y. We will also INSTALL express and mongoose in our Node.js app. Now, create a file config.js and in this file, we will store all the configuration related to our project. Here, we are storing the app and db configuration both, which in small projects we give in the root file only. Next, create a file db.js and in it we can use the configurations from config.js file DIRECTLY by just importing it and using it. Here, we are just de-structuring the config object and taking the db values from it. After that we are further de-structuring and taking the host, port, and name from it. After that create a file App.js and inside it alo, we are using the data from the config object in the config.js file. Now, when we run the node app.js command, we will see the port number correctly connecting. |
|