Skip to content

Commit 24e7e33

Browse files
committed
initial commit
0 parents  commit 24e7e33

File tree

7 files changed

+212
-0
lines changed

7 files changed

+212
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "web-workers",
3+
"main": "server.js",
4+
"dependencies": {
5+
"node-static-alias": "~1.1.2"
6+
}
7+
}

server.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
3+
var path = require("path");
4+
var http = require("http");
5+
var nodeStaticAlias = require("node-static-alias");
6+
7+
const PORT = 8049;
8+
const WEB_DIR = path.join(__dirname,"web");
9+
10+
var httpServer = http.createServer(handleRequest);
11+
12+
var staticServer = new nodeStaticAlias.Server(WEB_DIR,{
13+
serverInfo: "Web Workers",
14+
cache: 1
15+
});
16+
17+
18+
httpServer.listen(PORT);
19+
console.log(`Server started on http://localhost:${PORT}...`);
20+
21+
22+
// *******************************
23+
24+
async function handleRequest(req,res) {
25+
// handle static file requests
26+
if (["GET","HEAD"].includes(req.method)) {
27+
// special handling for empty favicon
28+
if (req.url == "/favicon.ico") {
29+
res.writeHead(204,{
30+
"Content-Type": "image/x-icon",
31+
"Cache-Control": "public, max-age: 604800"
32+
});
33+
res.end();
34+
return;
35+
}
36+
37+
// handle other static files
38+
staticServer.serve(req,res,function onStaticComplete(err){
39+
if (err) {
40+
res.writeHead(404);
41+
res.end();
42+
}
43+
});
44+
}
45+
// Oops, invalid/unrecognized request
46+
else {
47+
res.writeHead(404);
48+
res.end();
49+
}
50+
}

web/css/style.css

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
html {
2+
box-sizing: border-box;
3+
font-family: sans-serif;
4+
font-size: 1.3em;
5+
}
6+
7+
*,
8+
*::before,
9+
*::after {
10+
box-sizing: inherit;
11+
}
12+
13+
html input,
14+
html button,
15+
html textarea {
16+
font-size: 1em;
17+
}
18+
19+
html,
20+
body {
21+
background-color: #e5efff;
22+
}
23+
24+
header {
25+
position: relative;
26+
max-width: 800px;
27+
margin: 0px auto;
28+
color: #222;
29+
}
30+
31+
nav {
32+
background-color: #5078ba;
33+
color: #fff;
34+
}
35+
36+
nav ul {
37+
padding: 0px;
38+
padding-left: 20px;
39+
margin: 0px;
40+
list-style: none;
41+
}
42+
43+
nav ul li {
44+
display: inline-block;
45+
padding: 10px;
46+
}
47+
48+
nav ul li a {
49+
color: #fff;
50+
text-decoration: none;
51+
}
52+
53+
main {
54+
margin: 0px auto;
55+
max-width: 800px;
56+
background-color: #fff;
57+
color: #000;
58+
padding: 30px;
59+
}

web/index.html

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Web Workers</title>
6+
<link rel="stylesheet" href="/css/style.css">
7+
</head>
8+
<body>
9+
<header>
10+
<h1>Web Workers</h1>
11+
<nav>
12+
<ul>
13+
<li><a href="/">Home</a></li>
14+
</ul>
15+
</nav>
16+
</header>
17+
18+
<main>
19+
20+
<p>
21+
Let's explore Web Workers together.
22+
</p>
23+
<p>
24+
Fibonacci Numbers: <button type="button" id="start-stop-btn">Start</button>
25+
</p>
26+
<div id="fibs"></div>
27+
28+
</main>
29+
30+
<script src="/js/home.js"></script>
31+
</body>
32+
</html>

web/js/home.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
(function Home(){
2+
"use strict";
3+
4+
var startStopBtn;
5+
var fibsList;
6+
7+
document.addEventListener("DOMContentLoaded",ready,false);
8+
9+
10+
// **********************************
11+
12+
function ready() {
13+
startStopBtn = document.getElementById("start-stop-btn");
14+
fibsList = document.getElementById("fibs");
15+
16+
startStopBtn.addEventListener("click",startFibs,false);
17+
}
18+
19+
function renderFib(num,fib) {
20+
var p = document.createElement("div");
21+
p.innerText = `Fib(${num}): ${fib}`;
22+
if (fibsList.childNodes.length > 0) {
23+
fibsList.insertBefore(p,fibsList.childNodes[0]);
24+
}
25+
else {
26+
fibsList.appendChild(p);
27+
}
28+
}
29+
30+
function startFibs() {
31+
startStopBtn.removeEventListener("click",startFibs,false);
32+
startStopBtn.addEventListener("click",stopFibs,false);
33+
34+
startStopBtn.innerText = "Stop";
35+
fibsList.innerHTML = "";
36+
37+
// TODO
38+
}
39+
40+
function stopFibs() {
41+
startStopBtn.removeEventListener("click",stopFibs,false);
42+
startStopBtn.addEventListener("click",startFibs,false);
43+
44+
startStopBtn.innerText = "Start";
45+
46+
// TODO
47+
}
48+
49+
})();

web/js/worker.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
var curFib = 0;
4+
5+
// TODO
6+
7+
// **********************************
8+
9+
function fib(n) {
10+
if (n < 2) {
11+
return n;
12+
}
13+
return fib(n-1) + fib(n-2);
14+
}

0 commit comments

Comments
 (0)