-
Notifications
You must be signed in to change notification settings - Fork 16
/
server.js
55 lines (49 loc) · 1.05 KB
/
server.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
'use strict'
const http = require('http')
const fs = require('fs')
const pelo = require('.')
pelo.replace('bel')
const createApp = require('./app')
const app = createApp()
function layout (content) {
return `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
<style>
body {
font-family: sans-serif;
font-size: 32px;
}
</style>
</head>
<body>
<div id="root">
${content}
</div>
<script src="/bundle.js"></script>
</body>
</html>
`
}
const server = http.createServer((req, res) => {
if (req.url === '/bundle.js') {
res.writeHead(200, {
'Content-Type': 'text/javascript'
})
fs.createReadStream('./bundle.js').pipe(res)
return
}
const content = app.render()
const body = layout(content)
res.writeHead(200, {
'Content-Type': 'text/html'
})
res.end(body)
})
server.on('listening', () => {
console.log('Listening on', server.address());
});
server.listen(8080)