Environment variables, CORS, rate limiting, and deploying to Railway with a MongoDB Atlas connection.
npm install cors helmet express-rate-limitconst cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
// Security headers
app.use(helmet());
// CORS
app.use(cors({
origin: process.env.ALLOWED_ORIGINS?.split(',') || '*',
credentials: true,
}));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // 100 requests per window
message: { error: 'Too many requests' }
});
app.use('/api/', limiter);
// Auth rate limiter (stricter)
const authLimiter = rateLimit({ windowMs: 60*60*1000, max: 10 });
app.use('/api/auth/', authLimiter);PORT=3000
MONGODB_URI=mongodb+srv://...
JWT_SECRET=change-me-to-random-string
ALLOWED_ORIGINS=https://yourfrontend.com# railway.app → New Project → Deploy from GitHub
# Add environment variables in the Railway dashboard
# Railway auto-detects Node and runs: npm start
# package.json
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
}helmet() sets security headers with one line — always include it in production.origin to your actual frontend domain in production. Don't leave it as '*'..env. Use .env.example as a template with placeholder values.