Node: express, cors, helmet, morgangithub.com/sahilkhaire/gox/http
CORSOptions
Overview
CORSOptions configures the CORS middleware.
Part of the http package — Node.js analog: express, cors, helmet, morgan.
CORSOptions is a type exported by gox. Methods on this type are documented separately.
Signature
go
type CORSOptions struct {
AllowOrigins []string
AllowMethods []string
AllowHeaders []string
ExposeHeaders []string
AllowCredentials bool
MaxAge int
}Compare: Node.js · Standard Go · gox
js
// Typical express, cors, helmet, morgan pattern in Node.jsgo
func handler(w http.ResponseWriter, r *http.Request) {
// chi or net/http
}go
import "github.com/sahilkhaire/gox/http"
app := New()
app.Use(CORS(CORSOptions{AllowOrigins: []string{"https://app.example"}}))
app.Get("/", func(c *Ctx) error { return c.Status(204).JSON(204, nil) })
req := httptest.NewRequest(http.MethodOptions, "/", nil)
req.Header.Set("Origin", "https://app.example")
rec := httptest.NewRecorder()Example
go
import "github.com/sahilkhaire/gox/http"
app := New()
app.Use(CORS(CORSOptions{AllowOrigins: []string{"https://app.example"}}))
app.Get("/", func(c *Ctx) error { return c.Status(204).JSON(204, nil) })
req := httptest.NewRequest(http.MethodOptions, "/", nil)
req.Header.Set("Origin", "https://app.example")
rec := httptest.NewRecorder()Tips
Stack Logger, Recover, and Security middleware the way you would morgan + helmet in Express.
Standard library alternative
Use the standard library directly:
go
func handler(w http.ResponseWriter, r *http.Request) {
// chi or net/http
}