클라이언트와 서버를 완전히 분리하고자하므로 vue init webpack my-project
으로 vuejs 프로젝트를 만들었습니다. 이 프로젝트에서 나는프로덕션 환경의 vue-router
이 내 routes.js 파일입니다 ..이 /user/SOMEID
같은 특수 경로를 포함 (내 모든 라우팅 VUE 라우터를 사용하고 있습니다 :
import App from './App.vue'
export const routes = [
{
path: '/',
component: App.components.home
},
{
path: '/user/:id',
component: App.components.userid
},
{
path: '*',
component: App.components.notFound
}
]
나는 npm run dev
를 사용하여 응용 프로그램을 실행할 때 모든 것이 완벽하게 작동합니다. 이제 클라우드에 배포 할 준비가되었습니다. 따라서 npm run build
을 실행했습니다. HTTP 서버를 사용해야하기 때문에 Go를 사용하기로 결정했습니다.이 파일은 Go 파일입니다.
package main
import (
"fmt"
"github.com/go-chi/chi"
"github.com/me/myproject/server/handler"
"net/http"
"strings"
)
func main() {
r := chi.NewRouter()
distDir := "/home/me/code/go/src/github.com/me/myproject/client/dist/static"
FileServer(r, "/static", http.Dir(distDir))
r.Get("/", IndexGET)
http.ListenAndServe(":8080", r)
}
func IndexGET(w http.ResponseWriter, r *http.Request) {
handler.Render.HTML(w, http.StatusOK, "index", map[string]interface{}{})
}
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit URL parameters.")
}
fs := http.StripPrefix(path, http.FileServer(root))
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
}))
}
홈페이지를로드 할 수 있습니다 (App.components.home
) 모든 것이 작동하는 것처럼 보입니다 (CSS, 이미지, 번역, 서버 호출 및 응답). 그러나 404에서 결과를 가져 오거나 사용자를로드해야하는 다른 경로를 열려고하면 그저 응답 404 page not found
(일반 텍스트로 표시되는 vue notFound 구성 요소가 아닙니다) ..
어떤 아이디어가 잘못 되었습니까? 어떻게 수정해야합니까?
편집 : 이것은 main.js 파일의 라우터 설정의 다른 부분 :
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes
})
new Vue({
el: '#app',
router,
i18n,
render: h => h(App)
})
열려는 URL을 제공하십시오. 나는 Vue 경로를 사용하는 대신 Go 경로 (물론 하나가 빠진 경로)에 액세스하려고한다는 느낌이 들었습니다. – Alex
@Alex 슬프게도 URL을 제공 할 수 없습니다 (로컬에서 테스트하기). 당신이 말하는 것은 의미가 있습니다. 그렇다면 왜 localhost : 8080을 열고 App.components.home 페이지를 볼 수 있습니까? (다른 vue 구성 요소를 참조하는 데 문제가 없음)? – fisker