Skip to content

Commit

Permalink
Simple webapp for ch05s04 GE (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jramcast authored Nov 6, 2020
1 parent e13e839 commit 37415dc
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 0 deletions.
3 changes: 3 additions & 0 deletions simple-webapp/backend/greet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function greet(name) {
return `Hello ${name || "guest"}`;
}
16 changes: 16 additions & 0 deletions simple-webapp/backend/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const http = require("http");
const url = require("url");
const greet = require("./greet");

const server = http.createServer((req, res) => {
const { name } = url.parse(req.url, true).query;
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.setHeader("Access-Control-Allow-Origin", "*");
res.end(greet(name));
});

const port = 3000;
server.listen(port, () => {
console.log(`Server listening on ${port}`);
});
5 changes: 5 additions & 0 deletions simple-webapp/backend/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const assert = require("assert").strict;
const greet = require("./greet");

assert.strictEqual(greet(), "Hello guest");
assert.strictEqual(greet("Guy"), "Hello Guy");
30 changes: 30 additions & 0 deletions simple-webapp/backend/test_api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env sh

# Always set the current working directory as the project's root
PROJECT_ROOT=$(cd $(dirname $0)/.. && pwd)

# HTTP method
METHOD=$1

# Run server
node $PROJECT_ROOT/backend/server &
PID=$!

# Try to make a successful request
for i in {1..3}
do
curl -Is -X $METHOD http://localhost:3000 | grep 200
EXITCODE=$?

if [ $EXITCODE -eq 0 ]
then
break
fi

sleep 1
done

# Stop server
kill -15 $PID

exit $EXITCODE
16 changes: 16 additions & 0 deletions simple-webapp/frontend/greet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function greet(domInput, domElement) {
const api = greet.api || getGreeting;
return api(domInput.value).then(text => {
domElement.innerHTML = text;
});
}

function getGreeting(name) {
const url = "http://localhost:3000?name=" + name
return fetch(url)
.then(response => response.text());
}

if (typeof module !== "undefined") {
module.exports = greet;
}
9 changes: 9 additions & 0 deletions simple-webapp/frontend/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<body>
<h1 id="greeting"></h1>
<input id="username" type="text">
</body>
<script src="greet.js"></script>
<script src="main.js"></script>
</html>
6 changes: 6 additions & 0 deletions simple-webapp/frontend/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

document.addEventListener("DOMContentLoaded", () => {
const input = document.getElementById("username");
const greeting = document.getElementById("greeting");
input.addEventListener("keyup", () => greet(input, greeting));
});
13 changes: 13 additions & 0 deletions simple-webapp/frontend/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const assert = require("assert").strict;
const greet = require("./greet");

greet.api = async function apiMock(name) {
return `Hello ${name}`;
}

const input = { value: "Pablo" }
const h1 = { innerHTML: "" }

greet(input, h1).then(() => {
assert.strictEqual(h1.innerHTML, "Hello Pablo");
});

0 comments on commit 37415dc

Please sign in to comment.