Issue :
The project currently uses a config.js file for database connections and hardcodes the server IP in server.js. Hardcoding credentials and IP addresses is a security risk and makes the project difficult to deploy in different environments.
Improvement : Use environment variables (.env) for the database URI, port, and server address.
Updated server.js :
require('dotenv').config(); // Load environment variables
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { connectDB, sequelize } = require('./config/db');
// Routes and Models...
const app = express();
app.use(bodyParser.json());
app.use(cors());
connectDB();
sequelize.sync({ alter: true })
.then(() => console.log("Database synced successfully"))
.catch(err => console.error("Error syncing database:", err));
const PORT = process.env.PORT || 3001;
// Use an environment variable for the host or default to 0.0.0.0
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server running on port ${PORT}`);
});
Issue :
The project currently uses a
config.jsfile for database connections and hardcodes the server IP inserver.js.Hardcoding credentials and IP addresses is a security risk and makes the project difficult to deploy in different environments.Improvement : Use environment variables
(.env)for the database URI, port, and server address.Updated
server.js: