-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (50 loc) · 1.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import express from "express";
import bodyParser from "body-parser";
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
import analyticsRoutes from "./Routes/AnalyticsRoute.js";
import contactRoutes from "./Routes/ContactRoute.js";
import axios from "axios";
const app = express();
dotenv.config();
const corsOptions = {
origin: "https://picexpert.net",
};
app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
app.use(cors(corsOptions));
const port = process.env.PORT || 5000;
mongoose
.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() =>
app.listen(port || 5000, () =>
console.log(`Server running on http://localhost:${port}`)
)
)
.catch((error) => console.log(error.message));
app.use("/analytics", analyticsRoutes);
app.use("/contact", contactRoutes);
app.get("/fetch-image", async (req, res) => {
const { url } = req.query;
if (!url) {
return res.status(400).json({ message: "URL is required" });
}
try {
const response = await axios.get(url, { responseType: "arraybuffer" });
const contentType = response.headers["content-type"];
const buffer = Buffer.from(response.data, "base64");
res.set("Content-Type", contentType);
res.send(buffer);
} catch (error) {
return res
.status(403)
.json({ message: "Failed to fetch image from the provided URL." });
}
});
app.get("/", (req, res) => {
res.send("Hello to picExpert API");
});