-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp-code.js
76 lines (64 loc) · 1.71 KB
/
app-code.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
/**
* This is the app for code.highcharts.local
*/
// Modules sorted by names alphabetically
import express from 'express';
import fs from 'fs';
import http from 'http';
import https from 'https';
import { crtFile, codePort, codeSecurePort, pemFile } from './lib/arguments.js';
import { getCodeFile } from './lib/functions.js';
import { startWatchServer } from './lib/websocket.js';
import { extname } from 'path';
/**
* Create express application
*/
const app = express();
const options = {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
}
};
// Serve content of code directory
app.use('/', async (req, res) => {
const url = req.url.replace(/^\/(data-grid|gantt|maps|stock)\//g, '/');
const { error, content, path } = await getCodeFile(url, req);
if (error) {
res.end(error);
return;
}
if (content) {
const type = {
'.css': 'text/css',
'.js': 'text/javascript',
'.map': 'application/json',
'.svg': 'image/svg+xml'
}[extname(req.path)];
if (type) {
res.type(type);
}
res.send(content);
return;
}
res.sendFile(path, options);
});
/**
* Create HTTP server.
*/
const server = http.createServer(app);
server.listen(codePort);
/**
* Create HTTPS server.
*/
if (codeSecurePort) {
const httpsOptions = {
key: fs.existsSync(pemFile) && fs.readFileSync(pemFile, 'utf-8'),
cert: fs.existsSync(crtFile) && fs.readFileSync(crtFile, 'utf-8')
};
if (httpsOptions.cert && httpsOptions.key) {
https.createServer(httpsOptions, app).listen(codeSecurePort);
}
}
// WebSocket server for codeWatch
startWatchServer(server);