492 lines
8.7 KiB
Markdown
492 lines
8.7 KiB
Markdown
# Commands Cheatsheet
|
|
|
|
Quick reference for common commands when working with TurboTrades backend.
|
|
|
|
## 📦 Installation & Setup
|
|
|
|
```bash
|
|
# Install dependencies
|
|
npm install
|
|
|
|
# Copy environment variables
|
|
cp .env.example .env
|
|
|
|
# Edit environment variables
|
|
# Windows: notepad .env
|
|
# Mac/Linux: nano .env
|
|
```
|
|
|
|
## 🚀 Running the Server
|
|
|
|
```bash
|
|
# Development mode (auto-reload on file changes)
|
|
npm run dev
|
|
|
|
# Production mode
|
|
npm start
|
|
|
|
# With Node.js built-in watch mode
|
|
node --watch index.js
|
|
```
|
|
|
|
## 🗄️ MongoDB Commands
|
|
|
|
```bash
|
|
# Start MongoDB (Windows)
|
|
mongod
|
|
|
|
# Start MongoDB (Mac with Homebrew)
|
|
brew services start mongodb-community
|
|
|
|
# Start MongoDB (Linux systemd)
|
|
sudo systemctl start mongod
|
|
|
|
# Connect to MongoDB shell
|
|
mongosh
|
|
|
|
# Show databases
|
|
show dbs
|
|
|
|
# Use TurboTrades database
|
|
use turbotrades
|
|
|
|
# Show collections
|
|
show collections
|
|
|
|
# Find all users
|
|
db.users.find().pretty()
|
|
|
|
# Count users
|
|
db.users.countDocuments()
|
|
|
|
# Find user by Steam ID
|
|
db.users.findOne({ steamId: "76561198012345678" })
|
|
|
|
# Update user balance
|
|
db.users.updateOne(
|
|
{ steamId: "76561198012345678" },
|
|
{ $set: { balance: 100 } }
|
|
)
|
|
|
|
# Delete all users (be careful!)
|
|
db.users.deleteMany({})
|
|
|
|
# Create index on steamId
|
|
db.users.createIndex({ steamId: 1 }, { unique: true })
|
|
|
|
# Show all indexes
|
|
db.users.getIndexes()
|
|
```
|
|
|
|
## 🔧 NPM Commands
|
|
|
|
```bash
|
|
# Install new package
|
|
npm install package-name
|
|
|
|
# Install as dev dependency
|
|
npm install -D package-name
|
|
|
|
# Uninstall package
|
|
npm uninstall package-name
|
|
|
|
# Update all packages
|
|
npm update
|
|
|
|
# Check for outdated packages
|
|
npm outdated
|
|
|
|
# Audit security vulnerabilities
|
|
npm audit
|
|
|
|
# Fix vulnerabilities (if possible)
|
|
npm audit fix
|
|
|
|
# Clean install (delete node_modules and reinstall)
|
|
rm -rf node_modules package-lock.json
|
|
npm install
|
|
```
|
|
|
|
## 🧪 Testing Commands
|
|
|
|
```bash
|
|
# Test API health
|
|
curl http://localhost:3000/health
|
|
|
|
# Test with formatted JSON (requires jq)
|
|
curl http://localhost:3000/health | jq
|
|
|
|
# Test Steam login (opens browser)
|
|
curl http://localhost:3000/auth/steam
|
|
|
|
# Test authenticated endpoint (replace TOKEN)
|
|
curl http://localhost:3000/auth/me \
|
|
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
|
|
|
|
# Test with cookies
|
|
curl http://localhost:3000/auth/me \
|
|
-H "Cookie: accessToken=YOUR_TOKEN"
|
|
|
|
# Test POST request
|
|
curl -X POST http://localhost:3000/auth/logout \
|
|
-H "Cookie: accessToken=YOUR_TOKEN"
|
|
|
|
# Test PATCH request
|
|
curl -X PATCH http://localhost:3000/user/trade-url \
|
|
-H "Content-Type: application/json" \
|
|
-H "Cookie: accessToken=YOUR_TOKEN" \
|
|
-d '{"tradeUrl":"https://steamcommunity.com/tradeoffer/new/?partner=123&token=ABC"}'
|
|
```
|
|
|
|
## 🔌 WebSocket Testing
|
|
|
|
```bash
|
|
# Install wscat globally
|
|
npm install -g wscat
|
|
|
|
# Connect to WebSocket
|
|
wscat -c ws://localhost:3000/ws
|
|
|
|
# Connect with token
|
|
wscat -c "ws://localhost:3000/ws?token=YOUR_ACCESS_TOKEN"
|
|
|
|
# Send ping (after connecting)
|
|
{"type":"ping"}
|
|
|
|
# Send custom message
|
|
{"type":"custom","data":{"message":"hello"}}
|
|
|
|
# Disconnect
|
|
Ctrl+C
|
|
```
|
|
|
|
## 🐳 Docker Commands (Future)
|
|
|
|
```bash
|
|
# Build Docker image
|
|
docker build -t turbotrades .
|
|
|
|
# Run container
|
|
docker run -d -p 3000:3000 --env-file .env turbotrades
|
|
|
|
# Run with MongoDB
|
|
docker-compose up -d
|
|
|
|
# Stop containers
|
|
docker-compose down
|
|
|
|
# View logs
|
|
docker logs turbotrades
|
|
|
|
# Shell into container
|
|
docker exec -it turbotrades sh
|
|
|
|
# Remove container
|
|
docker rm -f turbotrades
|
|
|
|
# Remove image
|
|
docker rmi turbotrades
|
|
```
|
|
|
|
## 📊 PM2 Process Manager
|
|
|
|
```bash
|
|
# Install PM2 globally
|
|
npm install -g pm2
|
|
|
|
# Start application
|
|
pm2 start index.js --name turbotrades
|
|
|
|
# Start with environment
|
|
pm2 start index.js --name turbotrades --env production
|
|
|
|
# List processes
|
|
pm2 list
|
|
|
|
# Monitor processes
|
|
pm2 monit
|
|
|
|
# View logs
|
|
pm2 logs turbotrades
|
|
|
|
# View specific log lines
|
|
pm2 logs turbotrades --lines 100
|
|
|
|
# Restart application
|
|
pm2 restart turbotrades
|
|
|
|
# Stop application
|
|
pm2 stop turbotrades
|
|
|
|
# Delete from PM2
|
|
pm2 delete turbotrades
|
|
|
|
# Save process list
|
|
pm2 save
|
|
|
|
# Setup auto-start on boot
|
|
pm2 startup
|
|
|
|
# Update PM2
|
|
npm install -g pm2@latest
|
|
pm2 update
|
|
```
|
|
|
|
## 🔑 Generate Secrets
|
|
|
|
```bash
|
|
# Generate random secret (Node.js)
|
|
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
|
|
|
|
# Generate multiple secrets
|
|
node -e "for(let i=0;i<3;i++) console.log(require('crypto').randomBytes(32).toString('hex'))"
|
|
|
|
# Generate base64 secret
|
|
node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"
|
|
|
|
# On Linux/Mac with OpenSSL
|
|
openssl rand -hex 32
|
|
openssl rand -base64 32
|
|
```
|
|
|
|
## 🔍 Debugging
|
|
|
|
```bash
|
|
# Run with Node.js debugger
|
|
node --inspect index.js
|
|
|
|
# Run with Chrome DevTools
|
|
node --inspect-brk index.js
|
|
# Then open chrome://inspect in Chrome
|
|
|
|
# View all environment variables
|
|
node -e "console.log(process.env)"
|
|
|
|
# Check Node.js version
|
|
node --version
|
|
|
|
# Check npm version
|
|
npm --version
|
|
|
|
# Check MongoDB version
|
|
mongod --version
|
|
|
|
# View process on port
|
|
# Windows
|
|
netstat -ano | findstr :3000
|
|
|
|
# Mac/Linux
|
|
lsof -i :3000
|
|
|
|
# Kill process on port
|
|
# Windows
|
|
taskkill /PID <PID> /F
|
|
|
|
# Mac/Linux
|
|
kill -9 <PID>
|
|
```
|
|
|
|
## 📝 Git Commands
|
|
|
|
```bash
|
|
# Initialize git (if not already)
|
|
git init
|
|
|
|
# Check status
|
|
git status
|
|
|
|
# Add all files
|
|
git add .
|
|
|
|
# Commit changes
|
|
git commit -m "Your commit message"
|
|
|
|
# Create new branch
|
|
git checkout -b feature/new-feature
|
|
|
|
# Switch branches
|
|
git checkout main
|
|
|
|
# Push to remote
|
|
git push origin main
|
|
|
|
# Pull latest changes
|
|
git pull origin main
|
|
|
|
# View commit history
|
|
git log --oneline
|
|
|
|
# Undo last commit (keep changes)
|
|
git reset --soft HEAD~1
|
|
|
|
# Stash changes
|
|
git stash
|
|
|
|
# Apply stashed changes
|
|
git stash pop
|
|
```
|
|
|
|
## 🧹 Cleanup Commands
|
|
|
|
```bash
|
|
# Clear npm cache
|
|
npm cache clean --force
|
|
|
|
# Remove node_modules
|
|
# Windows
|
|
rmdir /s /q node_modules
|
|
|
|
# Mac/Linux
|
|
rm -rf node_modules
|
|
|
|
# Clear MongoDB database
|
|
mongosh turbotrades --eval "db.dropDatabase()"
|
|
|
|
# Clear PM2 logs
|
|
pm2 flush
|
|
|
|
# Clear all PM2 processes
|
|
pm2 kill
|
|
```
|
|
|
|
## 📈 Performance & Monitoring
|
|
|
|
```bash
|
|
# Check memory usage
|
|
node -e "console.log(process.memoryUsage())"
|
|
|
|
# Monitor CPU and memory (requires htop)
|
|
htop
|
|
|
|
# Node.js performance profiling
|
|
node --prof index.js
|
|
|
|
# Generate and view flamegraph
|
|
node --prof index.js
|
|
node --prof-process isolate-*.log > processed.txt
|
|
|
|
# Check MongoDB performance
|
|
mongosh --eval "db.currentOp()"
|
|
|
|
# MongoDB stats
|
|
mongosh turbotrades --eval "db.stats()"
|
|
```
|
|
|
|
## 🌐 Network Commands
|
|
|
|
```bash
|
|
# Check if port is open
|
|
# Windows
|
|
netstat -an | findstr :3000
|
|
|
|
# Mac/Linux
|
|
nc -zv localhost 3000
|
|
|
|
# Test WebSocket connection
|
|
curl -i -N -H "Connection: Upgrade" \
|
|
-H "Upgrade: websocket" \
|
|
-H "Sec-WebSocket-Version: 13" \
|
|
-H "Sec-WebSocket-Key: test" \
|
|
http://localhost:3000/ws
|
|
|
|
# Check DNS resolution
|
|
nslookup yourdomain.com
|
|
|
|
# Trace route
|
|
traceroute yourdomain.com # Mac/Linux
|
|
tracert yourdomain.com # Windows
|
|
```
|
|
|
|
## 🔐 SSL/TLS (Production)
|
|
|
|
```bash
|
|
# Generate self-signed certificate (development)
|
|
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
|
|
|
|
# Generate Let's Encrypt certificate (production)
|
|
sudo certbot certonly --standalone -d yourdomain.com
|
|
|
|
# Renew Let's Encrypt certificate
|
|
sudo certbot renew
|
|
|
|
# Check certificate expiry
|
|
openssl x509 -in cert.pem -text -noout
|
|
```
|
|
|
|
## 💡 Useful One-Liners
|
|
|
|
```bash
|
|
# Count lines of code
|
|
find . -name "*.js" -not -path "./node_modules/*" | xargs wc -l
|
|
|
|
# Find all TODO comments
|
|
grep -r "TODO" --exclude-dir=node_modules .
|
|
|
|
# Find large files
|
|
find . -type f -size +1M
|
|
|
|
# Backup database
|
|
mongodump --db turbotrades --out ./backup
|
|
|
|
# Restore database
|
|
mongorestore --db turbotrades ./backup/turbotrades
|
|
|
|
# Watch files for changes
|
|
watch -n 1 "ls -lh ."
|
|
|
|
# Continuous ping test
|
|
ping -c 10 yourdomain.com
|
|
|
|
# Get public IP
|
|
curl ifconfig.me
|
|
```
|
|
|
|
## 🎯 Quick Troubleshooting
|
|
|
|
```bash
|
|
# Server won't start - check if port is in use
|
|
lsof -i :3000 # Mac/Linux
|
|
netstat -ano | findstr :3000 # Windows
|
|
|
|
# MongoDB won't connect - check if running
|
|
mongosh --eval "db.version()"
|
|
|
|
# Permission denied - fix with chmod
|
|
chmod +x index.js
|
|
|
|
# EACCES error - don't use sudo, fix npm permissions
|
|
mkdir ~/.npm-global
|
|
npm config set prefix '~/.npm-global'
|
|
export PATH=~/.npm-global/bin:$PATH
|
|
|
|
# Module not found - reinstall dependencies
|
|
rm -rf node_modules package-lock.json
|
|
npm install
|
|
|
|
# Old cached data - clear cache
|
|
npm cache clean --force
|
|
rm -rf node_modules package-lock.json
|
|
npm install
|
|
```
|
|
|
|
## 📚 Documentation Links
|
|
|
|
- Fastify: https://www.fastify.io/docs/latest/
|
|
- MongoDB: https://www.mongodb.com/docs/
|
|
- Mongoose: https://mongoosejs.com/docs/
|
|
- Steam API: https://developer.valvesoftware.com/wiki/Steam_Web_API
|
|
- JWT: https://jwt.io/
|
|
- WebSocket: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
|
|
|
|
---
|
|
|
|
**Pro Tip**: Add commonly used commands to shell aliases!
|
|
|
|
```bash
|
|
# Add to ~/.bashrc or ~/.zshrc
|
|
alias dev="npm run dev"
|
|
alias start-mongo="brew services start mongodb-community"
|
|
alias stop-mongo="brew services stop mongodb-community"
|
|
alias logs="pm2 logs turbotrades"
|
|
```
|