-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (51 loc) · 1.66 KB
/
server.js
File metadata and controls
55 lines (51 loc) · 1.66 KB
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
import * as fs from 'fs';
import * as path from 'path';
import * as http from 'http';
import * as url from 'url';
import { generateHTML } from './publish.js'
http.createServer((request, response) => {
let uri = url.parse(request.url).pathname
//serve docs, like github
let filePath = path.join(process.cwd(), 'docs', uri.replace('/shibboleth', ''))
if (fs.existsSync(filePath)) {
if (fs.statSync(filePath).isDirectory()) {
filePath = path.join(filePath, "index.html")
let stat = fs.statSync(filePath)
response.writeHead(200, {
'Content-Type': "text/html",
'Content-Length': stat.size
})
let readStream = fs.createReadStream(filePath);
readStream.pipe(response);
} else {
let stat = fs.statSync(filePath)
let mime;
if ((/js$/).test(filePath)) {
mime = "text/javascript"
} else if ((/css$/).test(filePath)) {
mime = "text/css"
} else if ((/svg$/).test(filePath)) {
mime = "image/svg+xml"
} else if ((/ttf$/).test(filePath)) {
mime = "application/x-font-ttf"
} else if ((/webmanifest$/).test(filePath)) {
mime = "application/manifest+json"
} else if ((/json$/).test(filePath)) {
mime = "application/json"
} else {
mime = 'text/plain'
}
response.writeHead(200, {
'Content-Type': mime,
'Content-Length': stat.size
})
let readStream = fs.createReadStream(filePath);
readStream.pipe(response);
}
}
}).listen(3008)
const pugFile = path.resolve('./views/index.pug');
fs.watchFile(pugFile, {interval: 500}, () => {
generateHTML(pugFile)
});
generateHTML(pugFile);