-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·124 lines (100 loc) · 3.09 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
const port = 8080
import ip from 'ip'
import { readFileSync, readdirSync, lstatSync } from 'fs'
import { join } from 'path'
import express, { static as expressStatic, json, urlencoded } from 'express'
//const bodyParser = require('body-parser')
const myIP = ip.address()
const displayTypes = {
images: 'images',
externalImages: 'externalImages',
iframe: 'iframe',
}
let displayedFile = 'none.png'
let externalImage = ''
let displayedIframe = ''
let showType = displayTypes.images
const imgPath = 'img/'
let subDir = ''
//const getFrontendImagePath = () => `/${imgPath}${displayedFile}`
const getFrontendImagePath = () => `/${imgPath}${subDir}${displayedFile}`
const getServerImageDir = () => join('public', imgPath)
const getSelectedImageDir = () => join(getServerImageDir(), subDir)
const app = express()
app.use(expressStatic('public'))
app.use(json())
app.use(urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
/* potentiell loggen (wird bei jedem request durchgeführt) */
app.all('*', (req, _, next) => {
//console.log('connect!')
//console.log(req.method)
if (req.method === 'POST') {
//console.log(req.body)
}
next()
})
function sendHTML(res, path) {
res.set('Content-Type', 'text/html')
const html = readFileSync(path)
res.send(html)
}
app.get('/server', (_, res) => {
sendHTML(res, 'server.html')
})
app.get('/client', (_, res) => {
sendHTML(res, 'client.html')
})
app.get('/current', (_, res) => {
const response = {
img:
showType === displayTypes.images ? getFrontendImagePath() : externalImage,
iframe: displayedIframe,
show: showType,
}
res.send(response)
})
app.post('/update', (req, res) => {
displayedFile = req.body.img
;(showType = displayTypes.images), res.send('update successful')
})
app.post('/updateExt', (req, res) => {
externalImage = req.body.img
;(showType = displayTypes.externalImages), res.send('update successful')
})
app.post('/updateIframe', (req, res) => {
displayedIframe = req.body.iframe
showType = displayTypes.iframe
res.send('update successful')
})
app.post('/selectDir', (req, res) => {
const dir = req.body.dir
subDir = dir === '' ? dir : join(subDir, dir)
res.send('update successful')
})
app.get('/dirs', (_, res) => {
//const dirContents = fs.readdirSync(getServerImageDir())
const dirContents = readdirSync(getSelectedImageDir())
const folders = dirContents.filter((file) => {
const stat = lstatSync(join(getSelectedImageDir(), file))
return stat.isDirectory()
})
res.send(folders)
})
app.get('/images', (_, res) => {
//const dirContents = fs.readdirSync(getServerImageDir())
const dirContents = readdirSync(getSelectedImageDir())
const files = dirContents.filter((file) => {
if (file.startsWith('.')) {
return false
}
const stat = lstatSync(join(getSelectedImageDir(), file))
return !stat.isDirectory()
})
files.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
res.send(files)
})
app.listen(port)
console.log(
`listening on port ${port}. http://localhost:${port}/server http://${myIP}:${port}/client`
)