http server


Node.js

  1. const http = require('http')
  2. function handler(request, response) {
  3. response.writeHead(200, { 'Content-type':'text/plain' })
  4. response.write('hello world')
  5. response.end()
  6. }
  7. const server = http.createServer(handler)
  8. server.listen(8080)

Output

  1. $ curl http://localhost:8080
  2. hello world

Go

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

Output

  1. $ curl http://localhost:8080
  2. hello world