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

Skip to content

Commit cf268a0

Browse files
committed
add functionalities: '/addToState' 'resetState' 'sendState' 'substractFromState'
1 parent 2354641 commit cf268a0

17 files changed

+152
-950
lines changed

week1/index.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,56 @@
1+
var state = 10
12
import HTTP from 'http'
23
import Path from 'path'
34

45
import sendIndexHTML from './responses/sendIndexHTML'
56
import sendPage2HTML from './responses/sendPage2HTML'
67
import sendStylesCSS from './responses/sendStylesCSS'
78
import sendText from './responses/sendText'
9+
import sendState from './responses/sendState'
10+
import addToState from './responses/addToState'
811

912
const server = HTTP.createServer((request, response) => {
10-
console.log(request.method, request.url)
13+
console.log(request.method, request.url)
1114

12-
switch (request.url) {
13-
case '/':
14-
sendIndexHTML(response)
15-
break
16-
case '/page2':
17-
sendPage2HTML(response)
18-
break
19-
case '/styles.css':
20-
sendStylesCSS(response)
21-
break
22-
default:
23-
const extension = Path.extname(request.url)
24-
if (extension === '') {
25-
response.statusCode = 302
26-
response.setHeader('Location', '/')
27-
} else {
28-
response.statusCode = 404
29-
sendText(response, "File not found")
30-
}
31-
}
32-
33-
response.end()
15+
switch (request.url) {
16+
case '/':
17+
sendIndexHTML(response)
18+
break
19+
case '/page2':
20+
sendPage2HTML(response)
21+
break
22+
case '/styles.css':
23+
sendStylesCSS(response)
24+
break
25+
case '/state':
26+
sendState(response, state)
27+
break
28+
case '/add':
29+
state += 1
30+
addToState(response)
31+
break
32+
case '/remove':
33+
state -= 1
34+
renderState(response)
35+
break
36+
case '/reset':
37+
state = 10
38+
renderState(response)
39+
break
40+
default:
41+
const extension = request.url
42+
if (extension === '') {
43+
response.statusCode = 302
44+
response.setHeader('Location', '/')
45+
} else {
46+
response.statusCode = 404
47+
sendText(response, "Sorry... we couldn't find the link you are looking for.")
48+
}
49+
}
50+
51+
response.end()
3452
})
3553

36-
server.listen(3001)
54+
server.listen(8080)
3755

3856
console.log('Server started')

week1/responses/addToState.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function addToState(response) {
2+
response.setHeader('Content-Type', 'text/html')
3+
response.write(`
4+
<!html>
5+
<html>
6+
<head>
7+
<title>adding 1 to state</title>
8+
<link href="styles.css" type="text/css" rel="stylesheet"/>
9+
</head>
10+
<body>
11+
OK
12+
</body>
13+
</html>
14+
`)
15+
}

week1/responses/resetState.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function resetState(response) {
2+
response.setHeader('Content-Type', 'text/html')
3+
response.write(`
4+
<!html>
5+
<html>
6+
<head>
7+
<title>reset the value of state</title>
8+
<link href="styles.css" type="text/css" rel="stylesheet"/>
9+
</head>
10+
<body>
11+
OK
12+
</body>
13+
</html>
14+
`)
15+
}

week1/responses/sendIndexHTML.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
export default function sendIndexHTML(response) {
2-
response.setHeader('Content-Type', 'text/html')
3-
response.write(`
2+
response.setHeader('Content-Type', 'text/html')
3+
response.write(`
44
<!html>
55
<html>
66
<head>
77
<title>Hello</title>
88
<link href="styles.css" type="text/css" rel="stylesheet"/>
99
</head>
1010
<body>
11-
Hello I am a website
11+
Hello I am a website
1212
</body>
1313
</html>
1414
`)

week1/responses/sendPage2HTML.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default function sendPage2HTML(response) {
2-
response.setHeader('Content-Type', 'text/html')
3-
response.write(`
2+
response.setHeader('Content-Type', 'text/html')
3+
response.write(`
44
<!html>
55
<html>
66
<head>

week1/responses/sendState.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function sendState(response, state) {
2+
response.setHeader('Content-Type', 'text/html')
3+
response.write(`
4+
<!html>
5+
<html>
6+
<head>
7+
<title>the value of state</title>
8+
<link href="styles.css" type="text/css" rel="stylesheet"/>
9+
</head>
10+
<body>
11+
the value of state is:${state}
12+
</body>
13+
</html>
14+
`)
15+
}

week1/responses/sendText.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
export default function sendText(response, text) {
2-
response.setHeader('Content-Type', 'text/plain')
3-
response.write(text)
2+
response.setHeader('Content-Type', 'text/html')
3+
response.write(`
4+
<!html>
5+
<html>
6+
<head>
7+
<title>Sorry!!</title>
8+
<link href="styles.css" type="text/css" rel="stylesheet"/>
9+
</head>
10+
<body>
11+
${text}
12+
</body>
13+
</html>
14+
`)
415
}

week1/responses/substractFromState.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function substractFromState(response) {
2+
response.setHeader('Content-Type', 'text/html')
3+
response.write(`
4+
<!html>
5+
<html>
6+
<head>
7+
<title>substract 1 from value of state</title>
8+
<link href="styles.css" type="text/css" rel="stylesheet"/>
9+
</head>
10+
<body>
11+
OK
12+
</body>
13+
</html>
14+
`)
15+
}

week3/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ There are currently four actions:
2828
- `update` (`PUT /todos/:id`): Updates the description of a todo
2929
- `remove` (`DELETE /todos/:id`): Deletes a todo
3030

31+
Recently added:
32+
- `clear` (`DELETE /todos`): Clears all todos
33+
- `markAsDone` (POST /todos/:id/done`): Marks todo as done
34+
- `markAsNotDone` (DELETE /todos/:id/done`): Marks todo as not done
35+
3136
## Directory structure
3237

3338
- `actions`: Contains the actions as listed above, each as a express handler (function accepting request and response)

week3/actions/create.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const deserializeTodo = require('../util/deserializeTodo')
44
module.exports = function create(request, response) {
55

66
const todo = deserializeTodo(request, response)
7+
78
if (todo == null) { return }
89

910
Todo.create(todo.description, (error, todo) => {

0 commit comments

Comments
 (0)