Handle HTTP requests with style (in Javascript).
Javascript and Go belong together. I should be able to use npm modules inside my Go runtime.
// 1. Write your javascript modules. Example: jshandlers/index.js
// module.exports = {
// handle: function(request, response) {
// ResponseUtil(response).WriteString("Hello World, from Javascript")
// }
// }
// 2. Write your Go project
package main
import (
"fmt"
"net/http"
"github.com/didip/jazz"
"github.com/robertkrimen/otto"
)
func gohello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World, from Go")
}
func main() {
vm := otto.New()
// Configure all the global settings
jsutil.Configure(vm)
// Expected output: "Hello World, from Go"
http.HandleFunc("/", gohello)
// Expected output: "Hello World, from Javascript"
http.HandleFunc("/js", jazz.JSFuncHandler(vm, "jshandlers/index.js"))
http.ListenAndServe(":8080", nil)
}
-
Write request handlers in Javascript.
-
Javascript handlers are loaded during runtime, so you can refresh and see changes immediately. Just like PHP.
-
Able to require npm modules.
$NODE_PATH
is used to find modules. -
The javascript VM can further be enhanced to provide more functionalities.
-
You want to provide extensibility to your Go HTTP service.
-
You have this very important npm module that does not have Go equivalent.
-
You are just hacking for fun, wanting to see what's possible.
-
It complies to ECMAScript 5.1.
-
"use strict" will parse, but does nothing.
-
The regular expression engine (re2/regexp) is not fully compatible with the ECMA5 specification.