forked from yandex-code-pleinair/yandex-code-pleinair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
32 lines (30 loc) · 1.09 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
const http = require('http'),
fs = require('fs'),
path = require('path'),
HEADERS = {
'Content-Type': 'text/html',
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Resource-Policy': 'cross-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
'Origin-Agent-Cluster': '?1',
},
HTML_COMMON = `
<script>setInterval(() => window.parent.postMessage({ type: 'ping', url: window.location.toString() }, '*'), 2000)</script>
<style>body { margin: 0; width: 450px; height: 800px; }</style>
`
function runServer(name, port) {
http.createServer((req, res) => {
console.log(name, req.url)
fs.readFile(
path.join(__dirname, req.url === '/' ? '/index.html' : req.url),
(err, data) => {
err
? res.writeHead(500)
.end(`Error loading ${name}: ${err}`)
: res.writeHead(200, HEADERS)
.end(data + (name === 'sub' && req.url.endsWith('.html') ? HTML_COMMON : ''))
})
}).listen(port, () => console.log(`${name} server running at http://localhost:${port}`))
}
runServer('main', 3000)
runServer('sub', 4000)