Building an e-Commerce website with Node – Part 19 – Config file for log in

What is a config file?

Usually, a config file stores database information. We need to store for example, the port number, the secret key etc.

Let’s create a folder, in the project folder, to store all our config files. Name the folder “config”. Inside config folder create a new file and name it secret.js. This file will store the database information, port number, and secret key.

In the secret.js file type the following code:


module.exports = {
  database: 'mongodb://root:1234@ds161021.mlab.com:61021/wpmyeshop',
  port: 3000,
  secretKey: “saigonro@”
}

You need to export the file so other files (server.js in our case) can use it.
You need to cut the database information from server.js file and paste it in the secret.js.
The port number in our case is 3000.
At the secretkey field you have to type the server.js file in the middleware section at app.use(session ....

Next, in server.js file we have to require the secret.js file. On the top section of the server.js file, just above the var User = require('./models/user'); line type the following line of code:


var secret = require('./config/secret');

Next, in server.js file, where we cut the database information database: 'mongodb://root:1234@ds161021.mlab.com:61021/wpmyeshop' we have to type secret.database.

Next, in server.js file, where we cut the secret key ”saigonro@” we have to type secret.secretKey.

Next, in server.js file, where we cut the port number 3000 we have to type secret.port.

Now, you can open the terminal and check if everything is working fine.

Leave a Reply