Serving a single page application with go net/http mux

huangapple go评论112阅读模式
英文:

Serving a single page application with go net/http mux

问题

我正在构建一个同时为我的 React 前端应用提供服务的 API,但是在提供 index.html 文件时遇到了问题。

由于它不是一个 Go 模板,所以我没有使用 html/template。

我没有找到一种直接的方法来在所有不以 /api 开头的路由页面上提供应用的静态 HTML 根目录。

我故意不使用除了 gorilla 的 mux 之外的任何 Go 框架。

我的 handler.go 文件:

func Index(w http.ResponseWriter, r *http.Request) {
	http.FileServer(http.Dir("./views"))
}

routes.go 文件:

type Route struct {
	Name        string
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

type Routes []Route

var routes = Routes{
	Route{
		"Index",
		"GET",
		"/",
		Index,
	},
}

router.go 文件:

import (
	"net/http"

	"github.com/gorilla/mux"
)

func NewRouter() *mux.Router {

	router := mux.NewRouter().StrictSlash(true)
	for _, route := range routes {
		var handler http.Handler

		handler = route.HandlerFunc
		handler = Logger(handler, route.Name)

		router.
			Methods(route.Method).
			Path(route.Pattern).
			Name(route.Name).
			Handler(handler)

	}

	return router
}

main.go 文件:

package main

import (
	"./server"
	"log"
	"net/http"
)

func main() {

	router := server.NewRouter()

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

目前只显示一个空白页面。我的 index.html 文件位于可执行文件的 /views/index.html 目录下(我也尝试过与 handler 目录相关联)。

更新

我能够使用这个问题中显示的方法来提供 HTML 文件:https://stackoverflow.com/questions/26559557/how-do-you-serve-a-static-html-file-using-a-go-web-server。然而,使用 mux 和更模块化的文件结构仍然只会得到一个漂亮、干净的空白页面。

英文:

I'm building an api that also serves my react front end app, but am having an issue serving my index.html

Given that it's not really a go template I'm not using html/template.

I'm not seeing a strait forward way to serve the static html root of my app on all pages that do not start /api in the route.

I'm purposely trying not to use any go frameworks beyond gorilla's mux

My handler.go:

func Index(w http.ResponseWriter, r *http.Request) {
	http.FileServer(http.Dir("./views"))
}

Routes.go:

type Route struct {
	Name        string
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

type Routes []Route

var routes = Routes{
	Route{
		"Index",
		"GET",
		"/",
		Index,
	},
}

router.go

import (
	"net/http"

	"github.com/gorilla/mux"
)

func NewRouter() *mux.Router {

	router := mux.NewRouter().StrictSlash(true)
	for _, route := range routes {
		var handler http.Handler

		handler = route.HandlerFunc
		handler = Logger(handler, route.Name)

		router.
			Methods(route.Method).
			Path(route.Pattern).
			Name(route.Name).
			Handler(handler)

	}

	return router
}

main:

package main

import (
	"./server"
	"log"
	"net/http"
)

func main() {

	router := server.NewRouter()

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

Currently a blank page shows up, and thats it. My index.html is located in /views/index.html in relation to the executable (but I've tried it in relation to the handler as well)

Update

I was able to serve the html file using the method shown in this question: https://stackoverflow.com/questions/26559557/how-do-you-serve-a-static-html-file-using-a-go-web-server However using mux and the more modularized file structure still yields a nice pretty, clean blank page.

答案1

得分: 2

handler.go中,你的Index函数实际上是一个空操作,因为http.FileServer()返回一个Handler,该Handler从未接收到ResponseWriterRequest,因此导致页面为空白。

也许可以尝试像这样修改,以至少解决这个问题:

func Index(w http.ResponseWriter, r *http.Request) {
    http.FileServer(http.Dir("./views")).ServeHTTP(w, r)
}
英文:

In handler.go, your Index function is actually a no-op, since http.FileServer() returns a Handler, which is never passed the ResponseWriter or Request, hence the blank page.

Maybe try something like this to at least get past that:

func Index(w http.ResponseWriter, r *http.Request) {
    http.FileServer(http.Dir("./views")).ServeHTTP(w, r)
}

huangapple
  • 本文由 发表于 2016年1月19日 07:27:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/34866002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定