Skip to content

Commit bda0a42

Browse files
committed
Add UI page
1 parent 471a70b commit bda0a42

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

index.html

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Calculator</title>
6+
<style>
7+
html {
8+
color: #000;
9+
background-color: #fff;
10+
font-family: Arial, sans-serif;
11+
font-size: 2em;
12+
line-height: 1.1;
13+
}
14+
15+
h1 {
16+
font-size: 2em;
17+
font-weight: normal;
18+
margin-top:10px;
19+
margin-bottom:20px;
20+
}
21+
22+
input,select,button {
23+
text-align:right;
24+
margin-bottom:5px;
25+
font-size: 0.7em;
26+
}
27+
28+
#content {
29+
width:400px;
30+
margin:0px auto;
31+
text-align:center;
32+
}
33+
34+
#calculator {
35+
width:200px;
36+
margin:0px auto;
37+
text-align: right;
38+
margin-bottom: 10px;
39+
}
40+
41+
.error {
42+
color: #dd0000;
43+
}
44+
45+
img {
46+
display:block;
47+
margin-left: auto;
48+
margin-right: auto;
49+
}
50+
</style>
51+
</head>
52+
<body>
53+
<div id="content">
54+
<img src="https://s3-us-west-2.amazonaws.com/simple-calculator-website-demo/calculator.png"/>
55+
<h1>Simple Calculator Service</h1>
56+
<div id="calculator">
57+
<input id="a" type="text" name="a" size="8" /><br/>
58+
<select id="operation" name="operation">
59+
<option value="add" selected>+</option>
60+
<option value="subtract">-</option>
61+
<option value="multiply">*</option>
62+
<option value="divide">/</option>
63+
</select><br/>
64+
<input id="b" type="text" name="b" size="8" /><br/>
65+
<button id="calculate" name="calculate">Calculate</button><br/>
66+
</div>
67+
<span id="result"></span>
68+
</div>
69+
<script>
70+
var aInput = document.getElementById("a");
71+
var bInput = document.getElementById("b");
72+
var operationSelect = document.getElementById("operation");
73+
var calculateButton = document.getElementById("calculate");
74+
var resultInput = document.getElementById("result");
75+
calculateButton.addEventListener("click", function() {
76+
var operation = operationSelect.options[operationSelect.selectedIndex].value;
77+
var a = aInput.value;
78+
var b = bInput.value;
79+
var xhr = new XMLHttpRequest();
80+
xhr.open('GET', encodeURI('/' + operation + '?a=' + a + '&b=' + b));
81+
xhr.onload = function() {
82+
if (xhr.status === 200) {
83+
result.innerHTML = xhr.responseText;
84+
}
85+
else {
86+
result.innerHTML = '<span class="error">' + xhr.responseText + '<span>';
87+
}
88+
};
89+
xhr.send();
90+
});
91+
</script>
92+
</body>
93+
</html>

service.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express');
22
const calc = require('./calculator.js');
33
var numeral = require('numeral');
4+
var path = require('path');
45

56
const PORT = 8080;
67
const HOST = '0.0.0.0';
@@ -27,7 +28,7 @@ app.use(function(req, res, next) {
2728
});
2829

2930
app.get('/', function (req, res) {
30-
res.send('Simple Calculator Service!');
31+
res.sendFile(path.join(__dirname + '/index.html'));
3132
})
3233

3334
app.get('/add', function (req, res) {

0 commit comments

Comments
 (0)