UNPKG

api-explorer-cli

Version:

A CLI tool to proxy localhost API requests for the API Explorer web app

42 lines (35 loc) 1.08 kB
import express from 'express'; import axios from 'axios'; import cors from 'cors'; const app = express(); const port = process.env.PORT || 5000; app.use(cors({ origin: 'https://your-api-explorer.vercel.app', // Replace with your Vercel domain })); app.use(express.json()); // Endpoint to handle localhost proxy requests app.post('/api/proxy-localhost', async (req, res) => { const { url, method = 'GET', headers = {}, body } = req.body; if (!url || !url.startsWith('http://localhost')) { return res.status(400).json({ error: 'Invalid or non-localhost URL' }); } try { // Forward request to CLI script's proxy server const response = await axios.post('http://localhost:9999/proxy', { url, method, headers, body, }); res.json(response.data); } catch (error) { res.status(500).json({ error: 'Failed to connect to proxy server', details: error.message, }); } }); // Start the server app.listen(port, () => { console.log(`Backend running on port ${port}`); });