-
Notifications
You must be signed in to change notification settings - Fork 7
/
server-side.js
49 lines (42 loc) · 1.19 KB
/
server-side.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
var List = require('./list.js')
var http = require('http')
var browserify = require('browserify')
var path = require('path')
// Create our server
http.createServer(function (req, res) {
// Some crude routing
if (req.url === '/' || req.url === '/index.html') {
index(res)
} else if (req.url === '/bundle.js') {
script(res)
} else {
res.end('')
}
}).listen(1337)
console.log('Server running at http://localhost:1337/')
// Our index.html
function index (res) {
res.writeHead(200, {'Content-Type': 'text/html'})
// Initial data for the list
var data = ['This is', 'the initial', 'content']
// Create/render the list
var list = new List()
// Convert the list element to static HTML and send out
var html = list.toString(data)
// Serve up our template
var template = `<html><body>
${html}
<script src="/bundle.js"></script>
</body></html>`
res.end(template)
}
// Our script that will take over after the initial static render
function script (res) {
var b = browserify()
b.add(path.join(__dirname, 'standalone.js'))
b.bundle(function (err, buf) {
if (err) throw err
res.writeHead(200, {'Content-Type': 'application/javascript'})
res.end(buf)
})
}