Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4fd955

Browse files
committed
week3 homework
1 parent c80a9ac commit b4fd955

File tree

4 files changed

+135
-20
lines changed

4 files changed

+135
-20
lines changed

week3-homework.md

Lines changed: 0 additions & 20 deletions
This file was deleted.

week3/index.html

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>week 3 homework</title>
6+
</head>
7+
<body>
8+
<h3>My TO DO List</h3>
9+
<span id="get_list_button" class="button">Get To Dos</span>
10+
<div id="todo_list"></div>
11+
<span id="remove_all_button" class="button">Remove All</span>
12+
<h4>Add To Do</h4>
13+
<input type="text" name="New To DO" id="add_todo"><span id="add_todo_button" class="button">Add</span>
14+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
15+
<script type="text/javascript">
16+
// add js here
17+
$(document).ready(function(){
18+
var buttonCSS = {
19+
"outline-style": "solid",
20+
"outline-color": "purple",
21+
"outline-width": "1px",
22+
"color": "orange",
23+
"font-family": "sans-serif",
24+
"background-color": "green"
25+
};
26+
$("#get_list_button").click(function(){
27+
console.log("get list clicked");
28+
$.ajax({
29+
url: "http://localhost:8080/todos",
30+
method: "GET",
31+
success: showTodos
32+
})
33+
});
34+
$("#remove_all_button").click(function(){
35+
console.log("remove all clicked");
36+
$.ajax({
37+
url: "http://localhost:8080/todos",
38+
method: "DELETE",
39+
success: showTodos
40+
})
41+
});
42+
$("#add_todo_button").click(function(){
43+
console.log("add todo clicked")
44+
console.log("add this todo: " + $("#add_todo").val())
45+
$.ajax({
46+
url: "http://localhost:8080/todos",
47+
method: "POST",
48+
data: $("#add_todo").val(),
49+
processData: false,
50+
success: showTodos
51+
})
52+
});
53+
$(".button").css(buttonCSS);
54+
55+
function showTodos(data, status) {
56+
$("#todo_list").empty();
57+
$.each(data, function (i, val) {
58+
$("#todo_list").append(i + " " + val + " <span id='todo" + i + "'>Remove<br>");
59+
$("#todo" + i).css(buttonCSS);
60+
$("#todo" + i).click(function(){
61+
$.ajax({
62+
url: "http://localhost:8080/todos/" + i,
63+
method: "DELETE",
64+
success: showTodos
65+
})
66+
});
67+
});
68+
}
69+
});
70+
</script>
71+
</body>
72+
</html>

week3/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var fs = require('fs');
2+
var http = require('http');
3+
4+
var port = 8080;
5+
var server = http.createServer(handleRequest);
6+
7+
function handleRequest (req, res) {
8+
// console.log(req.url);
9+
switch (req.url) {
10+
case '/':
11+
fs.readFile(__dirname + '/index.html', 'utf-8', function (error, data) {
12+
if (error) {
13+
res.writeHead(500);
14+
res.write('Could not open file');
15+
} else {
16+
res.writeHead(200, {'Content-Type': 'text/html'});
17+
res.write(data);
18+
}
19+
res.end();
20+
});
21+
console.log('root');
22+
break;
23+
default :
24+
console.log("url: " + req.url + ", method: " + req.method);
25+
res.writeHead(200, {'Content-Type': 'application/json'});
26+
// res.write('{"1":"make tea","2":"do dishes","3":"go shopping"}');
27+
// res.write('{}');
28+
res.end();
29+
}
30+
}
31+
32+
server.listen(port, function (error) {
33+
if (error) {
34+
console.log(error);
35+
} else {
36+
console.log('listening on port 8080');
37+
}
38+
39+
});

week3/week3-homework.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Node JS Class
2+
3+
## Homework Week 3
4+
5+
### Create Node JS server that:
6+
7+
- Listens on port 8080
8+
9+
- Responds to these endpoints:
10+
- GET /todos/
11+
- Returns list of todos
12+
- POST /todos/
13+
- add a todo to the list and return the new list
14+
- DELETE /todos/`<todo index>`
15+
- remove a todo and return the new list
16+
- DELETE /todos/
17+
- clear all the todos from the list
18+
### Notes:
19+
20+
A front-end html page that makes the correct calls and displays the results is supplied.
21+
22+
Don't edit the html page. If you find en error in the page please let me know straight away so I can fix it for everyone.
23+
24+
I have supplied a skeleton index.js that serves the home page at http://localhost:8080/. You can build on it or create your own.

0 commit comments

Comments
 (0)