Spaces:
Running
Running
const express = require("express"); | |
const path = require("path"); | |
const axios = require("axios"); | |
const app = express(); | |
const port = process.env.PORT || 7860; | |
// Serve static files from the current directory | |
app.use(express.static(__dirname)); | |
// Route to fetch HTML content from a given URL | |
app.get("/fetch-html", async (req, res) => { | |
const url = req.query.url; | |
if (!url) { | |
return res.status(400).json({ error: "URL parameter is required" }); | |
} | |
try { | |
const response = await axios.get(url); | |
res.send(response.data); | |
} catch (error) { | |
res.status(500).json({ error: "Failed to fetch HTML content" }); | |
} | |
}); | |
// Send index.html for any other routes | |
app.get("*", (req, res) => { | |
res.sendFile(path.resolve(__dirname, "index.html")); | |
}); | |
app.listen(port, () => { | |
console.log(`Server is running on port ${port}`); | |
}); | |