Skip to content

sa08-frontend/Codvex-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌐 Visitor Tracker API

A production-ready Node.js + Express backend that tracks website visitors and sends real-time email notifications via Nodemailer.


📁 Project Structure

visitor-tracker/
├── server.js                  ← Entry point
├── routes/
│   └── track.js               ← POST /track endpoint
├── services/
│   ├── emailService.js        ← Nodemailer email sender
│   ├── parserService.js       ← UA / device / browser parser
│   └── cooldownService.js     ← In-memory spam prevention
├── .env.example               ← Copy → .env and fill values
├── .gitignore
└── package.json

🚀 Local Setup

# 1. Clone / download the project
cd visitor-tracker

# 2. Install dependencies
npm install

# 3. Set up environment variables
cp .env.example .env
# Edit .env with your email credentials

# 4. Start the server
npm start          # production
npm run dev        # development (auto-restart with nodemon)

🔌 API Reference

POST /track

Logs a visitor and sends an email notification (respecting cooldown).

Request body (JSON):

{
  "ip":        "203.0.113.42",
  "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ...",
  "platform":  "Win32",
  "timestamp": "2024-01-15T10:30:00.000Z",
  "location":  { "city": "Mumbai", "country": "India" }
}

Success response:

{ "success": true, "notified": true }

Cooldown response:

{ "success": true, "notified": false, "reason": "cooldown" }

GET /health

Returns server uptime — use this for Render's health check.

{ "status": "ok", "uptime": 42.3 }

🌐 Frontend Integration (Snippet)

Paste this in your website's HTML to call the tracker:

<script>
fetch("https://YOUR-RENDER-URL.onrender.com/track", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    ip:        "visitor-ip",       // fetch from ipapi.co or similar
    userAgent: navigator.userAgent,
    platform:  navigator.platform,
    timestamp: new Date().toISOString(),
    location:  null                // pass city/country if you have it
  })
});
</script>

To get the real visitor IP on the frontend, first call:

const { ip } = await fetch("https://api.ipapi.is/").then(r => r.json());

☁️ Deployment on Render (Free Tier)

Step 1 — Push code to GitHub

git init
git add .
git commit -m "initial commit"
git remote add origin https://github.com/YOUR_USERNAME/visitor-tracker.git
git push -u origin main

Step 2 — Create a new Web Service on Render

  1. Go to https://render.com and sign in
  2. Click "New""Web Service"
  3. Connect your GitHub repo
  4. Fill in the settings:
Setting Value
Name visitor-tracker
Environment Node
Build Command npm install
Start Command npm start
Instance Type Free

Step 3 — Add Environment Variables

In Render → your service → "Environment" tab, add:

Key Value
EMAIL_SERVICE gmail
EMAIL_USER you@gmail.com
EMAIL_PASS xxxx xxxx xxxx xxxx
NOTIFY_TO your-inbox@example.com
COOLDOWN_MINUTES 10

Step 4 — Deploy

Click "Create Web Service" — Render will build and deploy automatically. Your API will be live at:
https://visitor-tracker.onrender.com

Step 5 — Health Check (optional but recommended)

In Render → Settings → Health Check Path, set:
/health

This keeps your service alive and Render knows when to restart it.


📧 Gmail Setup (App Password)

Regular Gmail passwords don't work with SMTP. You need an App Password:

  1. Go to myaccount.google.com
  2. Security2-Step Verification → enable it
  3. SecurityApp Passwords
  4. Generate one for "Mail" / "Other"
  5. Copy the 16-char password → paste as EMAIL_PASS in .env

🛡️ Spam Prevention Logic

First visit from IP → ✅ Send email
Second visit within 10 min → ⏭️ Skip (cooldown)
Visit after 10+ minutes → ✅ Send email again

The cooldown duration is controlled by COOLDOWN_MINUTES in .env.


📝 License

MIT — free to use and modify.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors