-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function greet(name) { | ||
return `Hello ${name || "guest"}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |