Skip to main content
ParityEdge
Node.js (Express / Fastify)
Backend Runtimes

Node.js (Express, Fastify & NestJS) Backend Integration

Resolve visitor purchasing power parity pricing server-side in Node.js microservices, Express routes, and Fastify gateways before rendering checkout sessions.

Express.js Route Handler

Extract the visitor's real IP from x-forwarded-for or the TCP socket, and query the ParityEdge Edge API:

import express from 'express';

const app = express();
const EDGE_API_URL = 'https://api.parityedge.app/v1/lookup';
const PROJECT_ID = 'prj_live_YOUR_KEY';

app.get('/api/pricing', async (req, res) => {
  const clientIp = req.headers['x-forwarded-for'] || req.socket.remoteAddress;

  try {
    const edgeRes = await fetch(
      `${EDGE_API_URL}?projectId=${PROJECT_ID}`,
      {
        headers: {
          'x-forwarded-for': String(clientIp),
        },
      }
    );

    const parity = edgeRes.ok ? await edgeRes.json() : null;
    const basePrice = 49.00;
    const finalPrice = parity?.eligible
      ? basePrice * (1 - parity.discountPercentage / 100)
      : basePrice;

    res.json({
      basePrice,
      finalPrice,
      parity,
    });
  } catch (err) {
    res.json({ basePrice: 49.00, finalPrice: 49.00, parity: null });
  }
});

app.listen(3000, () => {
  console.log('Server listening on http://localhost:3000');
});

Fastify High-Throughput Route

In Fastify, use the native request.ip to resolve geo-located pricing in sub-10ms:

import Fastify from 'fastify';

const fastify = Fastify({ logger: true });
const EDGE_API_URL = 'https://api.parityedge.app/v1/lookup';

fastify.get('/api/pricing', async (request, reply) => {
  const clientIp = request.ip;

  const res = await fetch(`${EDGE_API_URL}?projectId=prj_live_YOUR_KEY`, {
    headers: { 'x-forwarded-for': clientIp },
  });

  const parity = res.ok ? await res.json() : null;
  return { basePrice: 49.00, parity };
});

fastify.listen({ port: 3000 });

Production Best Practices

Reverse Proxy Headers

When deployed behind reverse proxies or load balancers, ensure app.set('trust proxy', true) is configured in Express so req.ip accurately reflects the visitor's real geographic origin.

Edge Cache Inheritance

ParityEdge edge endpoints respond with Cache-Control: public, max-age=60. Reuse the payload across the visitor's session to minimize round-trips.