-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-demo.js
74 lines (61 loc) · 2.44 KB
/
server-demo.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
var port = process.argv[2] || 8888
var braid_text = require("./index.js")
// TODO: set a custom database folder
// (the default is ./braid-text-db)
//
// braid_text.db_folder = './custom_db_folder'
var server = require("http").createServer(async (req, res) => {
console.log(`${req.method} ${req.url}`)
// Free the CORS
braid_text.free_cors(res)
if (req.method === 'OPTIONS') return res.end()
if (req.url.endsWith("?editor")) {
res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
require("fs").createReadStream("./editor.html").pipe(res)
return
}
if (req.url.endsWith("?markdown-editor")) {
res.writeHead(200, { "Content-Type": "text/html", "Cache-Control": "no-cache" })
require("fs").createReadStream("./markdown-editor.html").pipe(res)
return
}
if (req.url == '/simpleton-client.js') {
res.writeHead(200, { "Content-Type": "text/javascript", "Cache-Control": "no-cache" })
require("fs").createReadStream("./simpleton-client.js").pipe(res)
return
}
// TODO: uncomment out the code below to add /pages endpoint,
// which displays all the currently used keys
//
// if (req.url === '/pages') {
// var pages = await braid_text.list()
// res.writeHead(200, {
// "Content-Type": "application/json",
// "Access-Control-Expose-Headers": "*"
// })
// res.end(JSON.stringify(pages))
// return
// }
// TODO: uncomment out the code below to add basic access control,
// where a user logs in by openning the javascript console
// and entering: document.cookie = 'fake_password'
//
// var admin_pass = "fake_password"
//
// if (req.method == "PUT" || req.method == "POST" || req.method == "PATCH") {
// if (!req.headers.cookie?.split(/;/).map(x => x.trim()).some(x => x === admin_pass)) {
// console.log("Blocked PUT:", { cookie: req.headers.cookie })
// res.statusCode = 401
// return res.end()
// }
// }
// Create some initial text for new documents
if (!(await braid_text.get(req.url, {})).version.length) {
await braid_text.put(req.url, {body: 'This is a fresh blank document, ready for you to edit.' })
}
// Now serve the collaborative text!
braid_text.serve(req, res)
})
server.listen(port, () => {
console.log(`server started on port ${port}`)
})