# Negroni — Idiomatic HTTP Middleware Library for Go > Negroni is a minimal, idiomatic Go library for composing HTTP middleware stacks, providing a thin layer between the net/http standard library and your application handlers. ## Install Save as a script file and run: # Negroni — Idiomatic HTTP Middleware Library for Go ## Quick Use ```bash go get github.com/urfave/negroni ``` ```go package main import ( "fmt" "net/http" "github.com/urfave/negroni" ) func main() { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello, Negroni!") }) n := negroni.Classic() n.UseHandler(mux) n.Run(":3000") } ``` ## Introduction Negroni is a small, non-intrusive middleware library for Go that embraces the standard net/http interfaces. Rather than replacing the standard library, it wraps it with a composable middleware stack so developers can layer logging, recovery, and custom handlers without learning a new API. ## What Negroni Does - Composes middleware functions into a sequential processing pipeline - Wraps any net/http handler or ServeMux without requiring a custom router - Ships with Classic() defaults: request logger, panic recovery, and static file server - Allows per-route middleware via integration with third-party routers like Gorilla Mux - Provides a Handler interface compatible with standard http.Handler for easy adoption ## Architecture Overview Negroni maintains an ordered slice of middleware handlers. Each middleware receives a ResponseWriter, Request, and a next function. Calling next passes control to the subsequent middleware or the final handler. This design is intentionally simple: no reflection, no dependency injection, and no hidden magic — just function composition around net/http types. ## Self-Hosting & Configuration - Requires Go 1.17 or later - Install with `go get github.com/urfave/negroni` - Create an instance with `negroni.New()` or `negroni.Classic()` for defaults - Add custom middleware with `n.Use()` or `n.UseFunc()` for simple functions - Bind to a port with `n.Run(":8080")` or attach to your own http.Server ## Key Features - Zero-magic design that follows Go idioms and net/http conventions - Classic() preset bundles production-ready logging and recovery - Works with any router (Gorilla Mux, Chi, httprouter, or the standard ServeMux) - Middleware ordering is explicit and predictable - Tiny codebase under 300 lines makes auditing and understanding straightforward ## Comparison with Similar Tools - **Gin** — full router with middleware; Negroni is middleware-only and router-agnostic - **Chi** — composable router with inline middleware; Negroni separates middleware from routing - **Alice** — similar middleware chaining; Negroni adds Classic() defaults and `Run()` - **Echo** — batteries-included framework; Negroni is deliberately minimal - **Martini** — by the same author but uses reflection; Negroni avoids reflection entirely ## FAQ **Q: Does Negroni include a router?** A: No. Negroni handles middleware composition only. Pair it with Gorilla Mux, Chi, or the standard ServeMux for routing. **Q: What is the Classic() preset?** A: Classic() creates a Negroni instance pre-loaded with Logger, Recovery, and Static middleware — suitable for most web applications out of the box. **Q: Can I use Negroni with Go modules?** A: Yes. The `github.com/urfave/negroni` path is fully compatible with Go modules. **Q: How does Negroni compare to writing raw middleware with net/http?** A: Negroni provides the same pattern but adds a structured stack, default handlers, and the convenience of `Run()`. It does not replace net/http — it wraps it. ## Sources - https://github.com/urfave/negroni - https://github.com/urfave/negroni#readme --- Source: https://tokrepo.com/en/workflows/asset-ff469060 Author: Script Depot