Gomiddlewarechain

An express.js-like-middleware-chain for julienschmidt's httprouter
Alternatives To Gomiddlewarechain
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Passport21,30081,2723,916an hour ago32May 20, 2022363mitJavaScript
Simple, unobtrusive authentication for Node.js.
Grant3,771210413 months ago98March 09, 202223mitJavaScript
OAuth Proxy
Everyauth3,480745385 years ago61October 17, 2014175JavaScript
node.js auth package (password, facebook, & more) for Connect and Express apps
Node Express Realworld Example App3,434
1a month ago1June 28, 201873JavaScript
Express Mongoose Es6 Rest Api2,896
3 years ago1September 03, 201668mitJavaScript
:collision: A boilerplate application for building RESTful APIs Microservice in Node.js using express and mongoose in ES6 with code coverage and JsonWebToken Authentication
Node Rate Limiter Flexible2,38968812 months ago146September 25, 20229iscJavaScript
Count and limit requests by key with atomic increments in single process or distributed environment.
Iron Session2,3848a day ago51September 09, 202259mitTypeScript
🛠 Next.js stateless session utility using signed and encrypted cookies to store data. Also works with Express, and Node.js HTTP servers
Foal1,7227222 months ago77May 29, 202222mitTypeScript
Full-featured Node.js framework, with no complexity. 🚀 Simple and easy to use, TypeScript-based and well-documented.
Permit1,664845 months ago8July 17, 20188mitJavaScript
An unopinionated authentication library for building Node.js APIs.
Accountill1,354
a day ago16mitJavaScript
Fullstack open source Invoicing application made with MongoDB, Express, React & Nodejs (MERN)
Alternatives To Gomiddlewarechain
Select To Compare


Alternative Project Comparisons
Readme

goMiddlewareChain Build Status

This is an express.js-like-middleware-chain for julienschmidt's httprouter

You can write your own middleware, and chain this to a lot of other middlewares (logging, auth,...).

Getting started

Install goMiddlewareChain

go get github.com/TobiEiss/goMiddlewareChain

Your first API

Here a simple example with a simple Ping-Pong-Handler chained with a JSONResponseHandler (from templates).

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/julienschmidt/httprouter"
	"github.com/TobiEiss/goMiddlewareChain"
	"github.com/TobiEiss/goMiddlewareChain/templates"
)

// Ping return a simply pong
func Ping(response *goMiddlewareChain.Response, request *http.Request, params httprouter.Params) {
	// simply pong
	response.Status.Code = http.StatusOK
	response.Data = "pong"
}

func main() {
	router := httprouter.New()
	router.GET("/api/v0/ping", goMiddlewareChain.RequestChainHandler(templates.JSONResponseHandler, Ping))

	log.Fatal(http.ListenAndServe(":8080", router))
}

After running this code, run curl localhost:8080/api/v0/ping in a terminal. You will get the following:

{
    "data":"pong",
    "msg":"OK",
    "status":200
}

Isn't it cool?

restricted-requestChainHandler

In some cases you need a restriction to apply requestChain. For example an auth-restriction. You can use the RestrictedRequestChainHandler. If the RestrictHandler failed, the code doesn't pass the chain.

Same example with Auth:

package main

import (
	"log"
	"net/http"

	"github.com/TobiEiss/goMiddlewareChain"
	"github.com/TobiEiss/goMiddlewareChain/templates"
	"github.com/julienschmidt/httprouter"
)

// Ping return a simply pong
func Ping(response *goMiddlewareChain.Response, request *http.Request, params httprouter.Params) {
	// simply pong
	response.Status.Code = http.StatusOK
	response.Data = "pong"
}

func Auth(response *goMiddlewareChain.Response, request *http.Request, params httprouter.Params) bool {
	user := request.Header.Get("X-User")
	return user == "HomerSimpson"
}

func main() {
	router := httprouter.New()
	router.GET("/api/v0/ping", goMiddlewareChain.RestrictedRequestChainHandler(Auth, templates.JSONResponseHandler, Ping))

	log.Fatal(http.ListenAndServe(":8080", router))
}

Now run curl --header "X-User: HomerSimpson" localhost:8080/api/v0/ping in your terminal. You will get:

{
    "data":"pong",
    "msg":"OK",
    "status":200
}

If you run curl --header "X-User: BartSimpson" localhost:8080/api/v0/ping, you get:

{
	"msg":"failed by passing restrictHandler",
	"status":401
}

handler from templates

You need more handler? Just let us now this.

Popular Express Projects
Popular Authentication Projects
Popular Frameworks Categories

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Go
Expressjs
Auth