|8. Environment Variables आणि Deployment
Chapter 8Node.js & Express~1 min read

Environment Variables आणि Deployment

Production Ready App

API keys, database passwords, port numbers — या sensitive values code मध्ये directly लिहू नयेत. Environment variables वापरा आणि .env file मध्ये store करा.

.env file

bash
# .env (NEVER commit to git!)
PORT=3000
MONGODB_URI=mongodb+srv://user:password@cluster.mongodb.net/myapp
JWT_SECRET=my_super_secret_key_here
NODE_ENV=development

dotenv वापरा

javascript
// npm install dotenv
// app.js च्या सुरुवातीला
require('dotenv').config();

const PORT = process.env.PORT || 3000;
const DB_URI = process.env.MONGODB_URI;

mongoose.connect(DB_URI);
app.listen(PORT, () => console.log(`Port: ${PORT}`));

package.json scripts

json
{
  "scripts": {
    "start": "node app.js",
    "dev": "nodemon app.js",
    "build": "echo 'No build step for Node'"
  }
}

Deployment Options

  • Railway.app — simplest, free tier available
  • Render.com — free tier, auto-deploys from GitHub
  • Heroku — classic option (now paid)
  • AWS EC2 / DigitalOcean — full control VPS
  • Vercel — mostly frontend, Edge Functions साठी

Key Points — लक्षात ठेवा

  • .env file — sensitive values store करा
  • process.env.KEY — environment variable वाचा
  • dotenv package — .env file load करतो
  • .gitignore मध्ये .env add करा (IMPORTANT!)
  • NODE_ENV=production — production mode
0/8 chapters पूर्ण