-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
49 lines (41 loc) · 1.28 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
42
43
44
45
46
47
48
49
import express from 'express';
import dotenv from 'dotenv';
import { createServer } from 'https';
import { readdir, readFileSync } from 'fs';
dotenv.config();
const app = express();
const baseDir = process.env.BASE_DIR;
app.get('/get-folders/:user', (req, res) => {
const { user } = req.params;
readdir(`${baseDir}/${user}/incoming/`,
{ withFileTypes: true },
(err, files) => {
let dirArray = [];
if (err) {
res.status(400).json({ code: 400, message: 'no such file or directory' });
} else {
files.forEach((file) => {
if (file.isDirectory()) {
dirArray.push(`${baseDir}/${user}/incoming/${file.name}`);
}
});
res.json(dirArray);
};
});
});
const port = process.env.PORT;
if (process.env.SSL_MODE === 'true') {
const privateKey = readFileSync(process.env.SSL_PRIVATE_KEY);
const sslCertificate = readFileSync(process.env.SSL_CERTIFICATE);
createServer({
key: privateKey,
cert: sslCertificate,
}, app).listen(port);
// eslint-disable-next-line no-console
console.log(`Folder getter app listening at https://localhost:${port}`);
} else {
app.listen(port, () => {
});
// eslint-disable-next-line no-console
console.log(`Folder getter app listening at http://localhost:${port}`);
}