-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
35 lines (30 loc) · 1014 Bytes
/
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
const express = require("express"); // Import the express module
const fs = require("fs"); // Import the fs module
const path = require("path"); // Import the path module
const app = express(); // Create an express app
const port = 3000; // Set the port
// Handle an HTTP GET request to the '/' endpoint
app.get("/", (req, res) => {
const filePath = path.join(__dirname, 'index.html');
res.sendFile(filePath);
});
app.get("/images", (req, res) => {
fs.readdir("outputs", (err, files) => {
if (err) {
// Handle error if the directory cannot be read
console.log(err);
res.sendStatus(500);
} else {
// Send the list of image file names as a JSON response
res.json(files);
}
});
});
// Handle an HTTP GET request to the '/outputs/*' endpoint
app.get("/outputs/:file", (req, res) => {
const filePath = path.join(__dirname, 'outputs', req.params.file);
res.sendFile(filePath);
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});