http server
# Node.js
const http = require("http");
function handler(request, response) {
response.writeHead(200, { "Content-type": "text/plain" });
response.write("hello world");
response.end();
}
const server = http.createServer(handler);
server.listen(8080);
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Output
$ curl http://localhost:8080
hello world
1
2
2
# Go
package main
import (
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("hello world"))
}
func main() {
http.HandleFunc("/", handler)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output
$ curl http://localhost:8080
hello world
1
2
2
编辑 (opens new window)
上次更新: 2022/09/30, 11:34:22