This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileServer.js
66 lines (57 loc) · 1.6 KB
/
fileServer.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
const express = require('express');
const app = express()
const cors = require('cors')
const server = require('http').createServer(app);
const parser = require('minimist')
const os = require('os');
const path = require('path');
app.use(cors())
const staticFilePath = path.join(__dirname, 'ui', 'dist-ui')
app.use('/gui', express.static(staticFilePath))
const standardFilePath = path.join(__dirname, 'images', 'standard')
app.use('/standard', express.static(standardFilePath))
app.get('/file', async function (req, res, next) {
var fileName = req.query.filename
if (typeof fileName === 'undefined') {
res.send(`no filename given`)
return
}
try {
res.sendFile(fileName, function (err) {
if (err) {
next(err)
} else {
}
})
} catch (error) {
res.send(`file not found: ${fileName}`)
}
})
class FileServer {
// 0 will result in a random open port being assigned
constructor(port = 0) {
this.cliArgs = parser(process.argv, { default: { port: 0 } })
this.port = this.cliArgs.port === 0 ? port : this.cliArgs.port
}
start() {
this.server = server.listen(this.port)
this.port = this.server.address().port // update port reference if it was randomly assigned
console.log(`fileServer listening at ${this.port}`)
process.send = process.send || function () {}
process.send(
{
type: 'port',
value: this.port
}
)
return this
}
quit() {
this.server.close((err) => {
process.exit(err ? 1 : 0)
})
}
}
module.exports.FileServer = FileServer
const fileServer = new FileServer()
fileServer.start()