This repository was archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (36 loc) · 1.34 KB
/
Copy pathserver.js
File metadata and controls
43 lines (36 loc) · 1.34 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
var st = require('st')
var http = require('http')
var path = require('path')
var fs = require('fs')
// See https://github.com/isaacs/st.
var mount = st({ path: __dirname, url: '', passthrough: true })
// We're statically serving the files in this folder. When requests are made
// to files that don't exist (ie, when we're accessing a club: /clubsatyale/ydn)
// we want to return 404.html with status 200. That's to mimick the way github
// pages work.
// See http://stackoverflow.com/a/10047018.
http.createServer((req, res) => {
function makeStaticServer(filename) {
return function () {
console.log('Serving '+filename+' for request to '+req.url)
var filePath = path.join(__dirname, filename)
var stat = fs.statSync(filePath)
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': stat.size
})
var readStream = fs.createReadStream(filePath)
// We replaced all the event handlers with a simple call to readStream.pipe()
readStream.pipe(res)
}
}
// Little twist: we must serve index.html when the person requests the
// root of the website.
if (req.url === "/") {
makeStaticServer('index.html')()
} else {
// If file not found... serve 404.html
mount(req, res, makeStaticServer('404.html'))
}
}).listen(8080);
console.log("Server is running.")