Skip to content

Commit 372b84e

Browse files
committed
http server
1 parent 2ee76e1 commit 372b84e

File tree

6 files changed

+128
-3
lines changed

6 files changed

+128
-3
lines changed

README.md

+86-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## Contents
66

77
- [Examples](#examples)
8+
- [comments](#comments)
89
- [print](#print)
910
- [arrays](#arrays)
1011
- [uint8 arrays](#uint8-arrays)
@@ -18,6 +19,35 @@
1819

1920
## Examples
2021

22+
All sample code is available in [examples/](examples/)
23+
24+
### Comments
25+
---
26+
27+
#### Node.js
28+
29+
```node
30+
// this is a line comment
31+
32+
/*
33+
this is a block comment
34+
*/
35+
```
36+
37+
#### Go
38+
39+
```go
40+
package main
41+
42+
func main() {
43+
// this is a line comment
44+
45+
/*
46+
this is a block comment
47+
*/
48+
}
49+
```
50+
2151
### Print
2252
---
2353

@@ -47,7 +77,7 @@ import "fmt"
4777
func main() {
4878
fmt.Println("hello world")
4979
fmt.Printf("hello %s\n", "world")
50-
fmt.Printf("hello %d %s", 5, "worlds")
80+
fmt.Printf("hello %d %s\n", 5, "worlds")
5181
}
5282
```
5383

@@ -362,6 +392,60 @@ Output
362392
hello world
363393
```
364394

395+
### http server
396+
---
397+
398+
#### Node.js
399+
400+
```node
401+
const http = require('http')
402+
403+
function handler(request, response) {
404+
response.writeHead(200, { 'Content-type':'text/plan' })
405+
response.write('hello world')
406+
response.end()
407+
}
408+
409+
const server = http.createServer(handler)
410+
server.listen(8080)
411+
```
412+
413+
Output
414+
415+
```bash
416+
$ curl http://localhost:8080
417+
hello world
418+
```
419+
420+
#### Go
421+
422+
```go
423+
package main
424+
425+
import (
426+
"net/http"
427+
)
428+
429+
func handler(w http.ResponseWriter, r *http.Request) {
430+
w.WriteHeader(200)
431+
w.Write([]byte("hello world"))
432+
}
433+
434+
func main() {
435+
http.HandleFunc("/", handler)
436+
if err := http.ListenAndServe(":8080", nil); err != nil {
437+
panic(err)
438+
}
439+
}
440+
```
441+
442+
Output
443+
444+
```bash
445+
$ curl http://localhost:8080
446+
hello world
447+
```
448+
365449

366450
<!--
367451
#### Node.js
@@ -385,6 +469,6 @@ Output
385469
```
386470
-->
387471

388-
# License
472+
## License
389473

390474
[MIT](LICENSE)

examples/comments.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
func main() {
4+
// this is a line comment
5+
6+
/*
7+
this is a block comment
8+
*/
9+
}

examples/comments.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// this is a line comment
2+
3+
/*
4+
this is a block comment
5+
*/

examples/http_server.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
func handler(w http.ResponseWriter, r *http.Request) {
8+
w.WriteHeader(200)
9+
w.Write([]byte("hello world"))
10+
}
11+
12+
func main() {
13+
http.HandleFunc("/", handler)
14+
if err := http.ListenAndServe(":8080", nil); err != nil {
15+
panic(err)
16+
}
17+
}

examples/http_server.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const http = require('http')
2+
3+
function handler(request, response) {
4+
response.writeHead(200, { 'Content-type':'text/plan' })
5+
response.write('hello world')
6+
response.end()
7+
}
8+
9+
const server = http.createServer(handler)
10+
server.listen(8080)

examples/print.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import "fmt"
55
func main() {
66
fmt.Println("hello world")
77
fmt.Printf("hello %s\n", "world")
8-
fmt.Printf("hello %d %s", 5, "worlds")
8+
fmt.Printf("hello %d %s\n", 5, "worlds")
99
}

0 commit comments

Comments
 (0)