-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (43 loc) · 1.69 KB
/
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
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 {filterImageFromURL, deleteLocalFiles} from './util/util.js';
// Init the Express application
const app = express();
// Set the network port
const port = process.env.PORT || 8082;
// Use the body parser middleware for post requests
app.use(bodyParser.json());
// GET /filteredimage?image_url={{URL}}
// endpoint to filter an image from a public url.
app.get('/filteredimage', async (req, res) => {
// const imageBuffer = ... // your image buffer
// const mimeType = mime.getType('image.jpg'); // replace 'image.jpg' with the actual file name or extension
const { image_url } = req.query;
// Check if the image_url query parameter is provided
if (!image_url) {
return res.status(400).send('Image URL is required');
}
try {
// Filter the image using the provided URL
const filteredImage = await filterImageFromURL(image_url);
// Send the filtered image as a response
res.sendFile(filteredImage, () => {
// Delete the local filtered image file after sending the response
deleteLocalFiles([filteredImage]);
});
} catch (error) {
console.log(error)
// Handle any errors that occur during image filtering
res.status(500).send('An error occurred while filtering the image');
}
});
// Root Endpoint
// Displays a simple message to the user
app.get( "/", async (req, res) => {
res.send("try GET /filteredimage?image_url={{}}")
} );
// Start the Server
app.listen( port, () => {
console.log( `server running http://localhost:${ port }` );
console.log( `press CTRL+C to stop server` );
} );