require('dotenv').config();
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Enable CORS and JSON parsing
app.use(cors());
app.use(express.json());
// Serve static frontend files from the "public" folder
app.use(express.static(path.join(__dirname, 'public')));
// Rate Limiter: Max 5 requests per minute per IP address
const apiLimiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 5,
message: { error: "Too many requests. Please wait a minute before checking again." }
});
// API Route to handle the DA/PA request securely
app.post('/api/check-authority', apiLimiter, async (req, res) => {
const { domain } = req.body;
if (!domain) {
return res.status(400).json({ error: "Please enter a valid domain name." });
}
// Clean up domain format (removes http://, https://, and spaces)
const cleanDomain = domain.replace(/^(https?:\/\/)?(www\.)?/, '').trim();
const options = {
method: 'GET',
url: `https://${process.env.RAPIDAPI_HOST}/?target=${cleanDomain}`, // URL changes slightly based on your chosen RapidAPI provider
headers: {
'X-RapidAPI-Key': process.env.RAPIDAPI_KEY,
'X-RapidAPI-Host': process.env.RAPIDAPI_HOST
}
};
try {
const response = await axios.request(options);
// Return the fresh DA/PA data straight to the user frontend
return res.json(response.data);
} catch (error) {
console.error("API Error:", error.message);
return res.status(500).json({ error: "Failed to fetch DA/PA data. Please try again later." });
}
});
app.listen(PORT, () => {
console.log(`Server running smoothly on http://localhost:${PORT}`);
});
