forked from shama/letswritecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (45 loc) · 1.46 KB
/
server.js
File metadata and controls
48 lines (45 loc) · 1.46 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
var http = require('http')
var fs = require('fs')
var browserify = require('browserify')
var db = require('level')('./db')
var template = require('lodash').template(fs.readFileSync('index.html', 'utf8'))
var sanitize = require('sanitize-html')
var server = http.createServer(function (req, res) {
res.setHeader('Content-Security-Policy', "script-src 'self' https://apis.google.com; style-src 'self'")
switch (req.url) {
case '/index.js':
// If requesting index.js, browserify our script and return it
browserify('index.js').bundle().pipe(res)
break
case '/index.css':
// If requesting index.css, just return that file
fs.createReadStream('index.css').pipe(res)
break
case '/addcomment':
// When adding a comment, put into the database
var comment = []
req.on('data', function (data) {
comment.push(data)
})
req.on('end', function () {
comment = comment.join('')
db.put(Date.now(), comment, function () {
res.end()
})
})
break
default:
// The default route is our index.html file, grab comments and
// feed to our template
var comments = []
db.createReadStream().on('data', function (data) {
comments.push(data.value)
}).on('end', function () {
res.end(template({comments:comments}))
})
break
}
})
server.listen(9966, function () {
console.log('Server running at http://locahost:9966')
})