-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy-server.js
38 lines (31 loc) · 1.13 KB
/
proxy-server.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
const express = require('express');
const fetch = require('node-fetch');
const cors = require('cors');
//import fetch from 'node-fetch';
//import cors from 'cors';
//import express from 'express';
const app = express();
const PORT = 3001; // Port on which the proxy server will run
app.use(cors()); // Enable CORS for all routes
app.get('/proxy-image', async (req, res) => {
try {
const { imageUrl } = req.query; // Get the image URL from the query parameters
if (!imageUrl) {
return res.status(400).send('Image URL is missing');
}
// Encode the image URL before making the request
const encodedImageUrl = encodeURIComponent(imageUrl);
// Fetch the image from the encoded URL
const response = await fetch(encodedImageUrl);
const buffer = await response.buffer();
// Set the appropriate content type header and send the image
res.setHeader('Content-Type', response.headers.get('content-type'));
res.send(buffer);
} catch (error) {
console.error(error);
res.status(500).send('Error fetching image');
}
});
app.listen(PORT, () => {
console.log(`Proxy server is running on port ${PORT}`);
});