我创造了路由有组织的方式.但它似乎没有到达我的controller
文件夹.我没有从控制器文件夹中得到响应.有人能告诉我这里犯了什么错误或错误吗?谢谢
这是主文件
https://go.dev/play/p/vSVk_78pcF4
在我的浏览器上,我正在走路由
localhost:8000/api/v1//tasks
和
http://localhost:8000/api/v1/tasks
但这两条路都告诉no page found
完整代码:
package main
import (
"fmt"
"net/http"
"github.com/sayedulkrm/go-curd-todo/routes"
)
func main() {
taskRouter := routes.TaskRoutes()
mainRouter := http.NewServeMux()
mainRouter.H和le("/api/v1/", taskRouter)
fmt.Printf("Server running on port 8000")
http.ListenAndServe(":8000", mainRouter)
}
// Routes folder ===================================
package routes
import (
"fmt"
"net/http"
"github.com/sayedulkrm/go-curd-todo/controllers"
)
func TaskRoutes() *http.ServeMux {
router := http.NewServeMux()
fmt.Printf("Heyyy Am getting called From TaskRoutes")
router.H和leFunc("GET /tasks", controllers.GetAllTasks)
return router
}
// Controllers Folder ===================================
package controllers
import (
"fmt"
"net/http"
)
func GetAllTasks(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Heyyy Am getting called From Controllers")
w.Write([]byte(`{"message": "working..."}`))
}
I want to make kinda like nested routes.
For example
从前端,我会击中
example.com/api/v1/task
我想在这里抛锚
main.go
`/api/v1` will be added in front of all routes i will create
so for example for order. it will be
`api/v1/orders`
For users:
`api/v1/users`