-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
41 lines (37 loc) · 1.01 KB
/
app.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
import express from 'express';
import fs from 'fs';
import path from 'path';
import { compressImage } from './compressAlgorithm.js';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const sourceDirectory = path.join(__dirname, './images');
const readImageFile = () => {
let image;
fs.readFile('test.jpg', (err, data) => {
if (err) throw err;
return data;
});
}
app.listen(5000, () => {
console.log(`Server is started on PORT ${5000}`);
try {
fs.readdir(sourceDirectory, (err, files) => {
if (err) {
throw new Error(err.message);
}
if (files && files.length > 0) {
files.forEach((file) => {
console.log(file)
fs.readFile(`${sourceDirectory}/${file}`, (err, data) => {
if (err) throw err.message
compressImage(data, file)
})
})
}
})
} catch (error) {
console.log(error.message);
}
});