-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (69 loc) · 2.22 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
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
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const path = require('path')
const homeDir = require('os').homedir()
const { getDirectoryDetails, errorLogPath } = require('./index.js');
function createHttpServer() {
const app = express();
app.use(bodyParser.json());
const corsOptions = {
origin: process.env.NODE_ENV === 'development' ? 'http://localhost:3000': 'http://localhost:5000'
}
app.get('/initialData', cors(corsOptions), async function (req, res) {
const initialDataPath = path.join(__dirname, './initialData.json');
try {
const data = require(initialDataPath);
if (data) {
res.json(data)
}
else {
res.status(500)
res.send("initialData.json file did not have key: initialPath")
}
}
catch (err) {
res.status(500)
res.send("Server can not read initialData.json file")
}
})
app.get('/', cors(corsOptions), async function (req, res) {
/* Nest boolean into an object to make it a pass by reference
rather than pass by value, and can then be checked at any
level of the recursive stack. Can't tell if ugly hack or
elegant way of cancelling a long-running recursive function. */
let requestStatus = { cancelled: false };
// Client cancels request
req.on('close', () => {
requestStatus.cancelled = true
})
const dirToAnalyze = req.query.path
if (dirToAnalyze) {
const pathPrefix = path.join(dirToAnalyze.replace('~', homeDir), '/')
let errors = []
const dir = await getDirectoryDetails(pathPrefix, errors, requestStatus)
if (!dir) {
res.status(404)
res.end()
return
}
if (errors.length > 0) {
dir.errors = errors
dir.logFile = errorLogPath
}
res.json(dir)
}
})
if (process.env.NODE_ENV !== 'development') {
app.use(express.static(path.join(__dirname, 'app/build')));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'app/build/index.html'));
});
}
const port = 8002
const server = app.listen(port, () => {
// console.log(`listening on localhost:${port}`)
});
return server
}
module.exports = createHttpServer