加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

高龙角2

发布时间:2020-12-17 09:17:45 所属栏目:安全 来源:网络整理
导读:我开始尝试使用Go和Angular2,但我有一个奇怪的问题..我想我只是错过了一个细微的细节,但我无法理解. 我正在使用https://github.com/julienschmidt/httprouter作为路由器Go …现在用Angular2,我应该能够复制将URL粘贴到浏览器中,Angular应该处理相应的路由,对
我开始尝试使用Go和Angular2,但我有一个奇怪的问题..我想我只是错过了一个细微的细节,但我无法理解.

我正在使用https://github.com/julienschmidt/httprouter作为路由器Go …现在用Angular2,我应该能够复制&将URL粘贴到浏览器中,Angular应该处理相应的路由,对吧?

我有一个“/登录”路由.如果通过前端访问路由,则可以使用该功能….但是如果我在浏览器中输入“mypage.com/login”,则获取404.

去路由基本上只是做

router.NotFound = http.FileServer(http.Dir("./public"))

它适用于“/”路由,但不适用于任何其他路由.这似乎是正确的.但是如何正确设置路由,所以Angular2处理所有路由?

问候

这是我正在使用的标准Go库,路由工作很棒.

查看Adapt function here

// Creates a new serve mux
mux := http.NewServeMux()

// Create room for static files serving
mux.Handle("/node_modules/",http.StripPrefix("/node_modules",http.FileServer(http.Dir("./node_modules"))))
mux.Handle("/html/",http.StripPrefix("/html",http.FileServer(http.Dir("./html"))))
mux.Handle("/js/",http.StripPrefix("/js",http.FileServer(http.Dir("./js"))))
mux.Handle("/ts/",http.StripPrefix("/ts",http.FileServer(http.Dir("./ts"))))
mux.Handle("/css/",http.StripPrefix("/css",http.FileServer(http.Dir("./css"))))

// Do your api stuff**
mux.Handle("/api/register",util.Adapt(api.RegisterHandler(mux),api.GetMongoConnection(),api.CheckEmptyUserForm(),api.EncodeUserJson(),api.ExpectBody(),api.ExpectPOST(),))
mux.HandleFunc("/api/login",api.Login)
mux.HandleFunc("/api/authenticate",api.Authenticate)

// Any other request,we should render our SPA's only html file,// Allowing angular to do the routing on anything else other then the api    
// and the files it needs for itself to work.
// Order here is critical. This html should contain the base tag like
// <base href="/"> *href here should match the HandleFunc path below 
mux.HandleFunc("/",func(w http.ResponseWriter,r *http.Request) {
    http.ServeFile(w,r,"html/index.html")
})

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读