Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤔 Middleware seems to run earlier than wildcard routing #207

Closed
hyingreborn opened this issue Mar 5, 2020 · 4 comments
Closed

🤔 Middleware seems to run earlier than wildcard routing #207

hyingreborn opened this issue Mar 5, 2020 · 4 comments

Comments

@hyingreborn
Copy link

Question description
Middleware seems to run earlier than wildcard routing,I use a custom middleware in router.Group,finally register a Not found route。

when i visit the "http://127.0.0.1:8080/api/v1/anyPathNotExist",server still exec the JWTAuth() first,But my expectation is that the return route doesn't exist

Code snippet (optional)

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()
  // General routing and using middleware in the group
	apiRouter := app.Group("/api")
	{
		authRouter := apiRouter.Group("/auth")
		{
			authRouter.Post("/login", auth.Login)
			authRouter.Get("/tokenRefresh", middleware.JWTAuth(), auth.TokenRefresh)
		}
		v1Router := apiRouter.Group("/v1", middleware.JWTAuth())
		{
			v1Router.WebSocket("/ws", websocket.WS)
			myRouter := v1Router.Group("/my")
			{
				myRouter.Get("/assets", my.Assets)
				myRouter.Get("/changePassword", my.ChangePassword)
			}
		}
	}

	// Not found route finally
	app.All("", func(c *fiber.Ctx) {
		c.Status(404).JSON("not found")
	})
}
@Fenny
Copy link
Member

Fenny commented Mar 6, 2020

Routes are executed in order #204
So in your case:

app.Group("/api", Auth())
// equals
app.Use("/api", Auth())

This means that every route starting with /api will execute that handler.

If I'm correct, you only want to execute the middleware handler if the child route exists?

@hyingreborn
Copy link
Author

@Fenny Yes ! Since child route doesn't exist, my expectation is not to execute any middleware, but to use a generic route to return 404 directly。
Although I registered the "/api" route group, it was not explicit to register a nonexistent child route such as "/api/notexist"

@Fenny
Copy link
Member

Fenny commented Mar 7, 2020

Our routing logic is based on the express framework and works the same.

GET /api/example
// "middleware!"
// 404 "Not Found"
const express = require('express')
const app = new express()

app.use('/api', (req, res, next) => {
    console.log('middleware!')
    next()
})
app.get('/api/login', (req, res, next) => {
    res.send("Welcome user!")
})
app.use('*', (req, res) => {
    res.sendStatus(404);
})

app.listen(3001)

When using Use() it will match everything starting with the provided 'prefix' path. It cannot see if a route will match later in the stack, so it will execute the attached handler either way.

If you really want to execute the middleware only when a route is found, I suggest attaching the middleware to a specific route directly.

app.Post("/login", JWT(), Login())

@hyingreborn
Copy link
Author

Thanks! It seems to be a feature rather than a problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants