DEV Community

Алексей Спинов
Алексей Спинов

Posted on

Build a Competitor Price Monitor in 50 Lines of Node.js

Track competitor prices automatically with zero dependencies.

The Script

const https = require('https');
const fs = require('fs');

const competitors = [
  { name: 'Store A', url: 'https://example.com/product-a' },
  { name: 'Store B', url: 'https://example.com/product-b' }
];

async function getPrice(url) {
  return new Promise((resolve) => {
    https.get(url, { headers: { 'User-Agent': 'PriceBot/1.0' } }, (res) => {
      let html = '';
      res.on('data', (d) => html += d);
      res.on('end', () => {
        const match = html.match(/\$([\d,.]+)/);
        resolve(match ? parseFloat(match[1].replace(',','')) : null);
      });
    }).on('error', () => resolve(null));
  });
}

async function monitor() {
  const file = 'prices.json';
  const history = fs.existsSync(file) ? JSON.parse(fs.readFileSync(file)) : [];

  for (const c of competitors) {
    const price = await getPrice(c.url);
    const prev = history.filter(h => h.name === c.name).pop();

    if (prev && prev.price !== price) {
      console.log(`CHANGE: ${c.name} ${prev.price} -> ${price}`);
    }

    history.push({ ...c, price, date: new Date().toISOString() });
  }

  fs.writeFileSync(file, JSON.stringify(history, null, 2));
}

monitor();
Enter fullscreen mode Exit fullscreen mode

Run It

node monitor.js
# Add to crontab for automatic monitoring:
# 0 */6 * * * node /path/to/monitor.js
Enter fullscreen mode Exit fullscreen mode

Pro Tips

  1. Check for JSON APIs first — many sites load prices via XHR. DevTools > Network > XHR.
  2. Add random delays between requests to avoid blocks
  3. Use Promise.allSettled() when monitoring many competitors in parallel
  4. Validate prices — if a price is $0 or $999999, something broke

More Resources


Need a custom price monitoring system? $50-100. Scraper + database + email alerts. 77 production scrapers. Email: Spinov001@gmail.com | Hire me

Top comments (0)