-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (38 loc) · 1.15 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
// Requiring module
import express from "express";
import fs from "fs";
// Creating express object
const app = express();
// Defining port number
const PORT = 3000;
// Function to serve all static files
// inside public directory.
app.use(express.static("public"));
app.use("/images", express.static("images"));
app.get("/gallery", (req, res) => {
let responseHTML = "<h1> your images </h1>";
fs.readdir("./public/images", (error, files) => {
var imgFiles = [];
let filesReverse = files.reverse();
filesReverse.forEach((file, index) => {
if (index > 10) {
//TODO: deleteFile(file);
}
responseHTML += `<img src='/images/${file}' />`;
});
// res.send(files[files.length-1]);
res.set("Content-Type", "text/html");
res.send(Buffer.from(responseHTML));
});
});
app.get("/latest-image", (req, res) => {
fs.readdir("./public/images", (error, files) => {
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ image: "/images/" + files[files.length - 1] }));
res.send();
});
});
// Server setup
app.listen(PORT, () => {
console.log(`Running server on PORT ${PORT}...`);
});