Express.js and Sequalize (for mysql)
I am learning how to set up a node server with express routing, and mysql for the database. (Am I leaving laravel?!?)
Everything was running smoothly until I started to set up the database configuration. As I explored in [Status Being Monitored](/status-being-monitored), keeping sensitive information out of version control is crucial for security. This relates to how I handled sensitive variables like passwords and database credentials. I do not want the sensitive info like passwords, etc in my version control … that stuff is secret you know. So I installed npm dotenv, and tested how to work it. It seemed simple enough, using the process.env.yourVariableName.
The problem cane when I set up sequelize. I used the cli, and that very nicely set things up for me. It set up a config.json in a config directory. But the json format could not use the process.env.yourVariableName. I did a lot of rooting around and trying the different suggestions that I found.
Finally, I found a good suggestion on their site near the bottom. after combing the docs, I found you could use a config.json file or alternatively a config.js file. The cli automatically installs the config.json. I removed it and used config.js in this format:
const dotenv = require('dotenv').config();
module.exports = {
development: {
database: process.env.DB_NAME,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: 'mysql',
},
}
With dotenv configured correctly, and building on ideas from [Start Up A New Hugo Installation](/start-up-new-hugo), the whole thing just magically worked!! And having set up my dotenv variables correspondingly, the whole thing just magically worked!!
Now I feel so safe about using it all. And in fact,I was glad I had experience with other database libraries like MySQL, which I wrote about in Coming Back, making it easier for me to get up to speed with Sequelize's documentation. I rather feel confortable now after about twenty minutes getting warmed up to their docs.
Get My Free E-book
Cleaner Living: Breathe through life's difficulties
Learn the scanning breath technique that helped me dissolve decades of emotional baggage. Discover practical tools you can use anywhere to process difficult emotions and find lasting well-being.
We respect your privacy. Your email will only be used to send the download link.