Skip to content

Commit 976e8f2

Browse files
committed
Updated codebase with button example.
1 parent 41ddbd5 commit 976e8f2

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ npm start
4141

4242
This launches `app.js`. To view the application in action, open your web browser, and navigate to `http://localhost:4000`.
4343

44+
Next, in your browser navigate to `http://localhost:4000/button.html`. This webpage contains a button that, when clicked, will send a HTTP GET request (by using AJAX) to app.js at `htttp://localhost:4000`. The response output will be written onto the webpage beneath the button.
45+
4446
## What Happened
4547

4648
`app.js` starts up a web server on port 4000, initialised with the Express framework. The code listens for incoming HTTP `GET` requests to the root of the application `/`, i.e. no other path. The incoming HTTP requests are represented by the `req` object.

Diff for: app.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const express = require('express')
22
const app = express()
33
const port = 4000
4+
//Tell Express to serve static html files from the 'public' folder.
5+
app.use(express.static('public'))
46

57

68
//Configure express to listen for HTTP GET requests at "/"
@@ -21,7 +23,7 @@ app.get("/", (req, res) => {
2123
//Write the output data into the HTTP response object, 'res'.
2224
res.write(data)
2325
//Send the HTTP response back to the client and close the connection.
24-
res.end("end");
26+
res.end();
2527
});
2628
})
2729

Diff for: public/button.html

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<html>
2+
<head>
3+
<title>
4+
My Button Example with Express
5+
</title>
6+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
7+
<script type="text/javascript">
8+
$(document).ready(function() {
9+
$('#server_button').click(function() {
10+
$.get('http://localhost:4000/', function(data) {
11+
$('#response_data').html(data); // show the list
12+
});
13+
});
14+
});
15+
</script>
16+
</head>
17+
<body>
18+
<h1>
19+
Team ArduinoPi - Uber Button
20+
</h1>
21+
<button type="button" name="button" id="server_button">Click me and talk to Express!</button>
22+
<div id="response_data" />
23+
</body>
24+
</html>

0 commit comments

Comments
 (0)