HTTP package guide
The gox/http package mirrors Express.js patterns: apps, routers, middleware, JSON handlers, and built-in security helpers.
Core routing
| Express | gox |
|---|---|
express() | http.New() |
app.get('/path', handler) | app.Get("/path", handler) |
res.json(data) | return c.JSON(200, data) |
req.body | c.BindJSON(&v) |
req.params.id | c.Param("id") |
See: http.New, http.Ctx.JSON
Middleware
| npm | gox |
|---|---|
cors | http.CORS |
helmet | http.Security |
morgan | http.Logger |
| error handler | Return error from handlers; use gox/err for status codes |
Advanced features
| npm | gox |
|---|---|
multer | http.ParseMultipart, http.SaveUploadedFile |
express-session | http.SessionMiddleware, http.MemoryStore |
| Server-Sent Events | http.SSE, http.SSEHandler |
express-rate-limit | http.RateLimit |
| WebSocket upgrade | http.HandleWS, gox/ws |
Example
go
import (
goxerr "github.com/sahilkhaire/gox/err"
goxhttp "github.com/sahilkhaire/gox/http"
)
app := goxhttp.New()
app.Use(goxhttp.Logger(), goxhttp.Recover(), goxhttp.Security())
app.Get("/health", func(c *goxhttp.Ctx) error {
return c.JSON(200, map[string]string{"status": "ok"})
})
app.Post("/users", func(c *goxhttp.Ctx) error {
var u struct{ Name string `json:"name"` }
if err := c.BindJSON(&u); err != nil {
return goxerr.BadRequest("invalid json")
}
return c.JSON(201, u)
})See the Express migration guide for a full side-by-side walkthrough.