英文:
Serving static files recursively with go
问题
我正在使用SvelteKit作为我正在开发的应用程序的前端。我在svelte.config.js中选择了一个静态适配器,它将所有必要的组件输出为静态文件。
这是我的静态目录的结构。
最上层的文件可以加载,但是static/_app目录中的文件无法加载。
func main() {
    router := mux.NewRouter()
    router.Handle("/", http.FileServer(http.Dir("static")))
    router.HandleFunc("/large-files", largeFilesHandler).Methods("OPTIONS", "POST")
    router.HandleFunc("/delete-file", deleteFileHandler).Methods("DELETE", "OPTIONS")
    logrus.SetLevel(logrus.ErrorLevel)
    err := http.ListenAndServe(":8000", router)
    if err != nil {
        logrus.Fatal(err)
    }
}
在我的svelte.config.js中:
import adapter from '@sveltejs/adapter-static';
import preprocess from 'svelte-preprocess';
const config = {
    preprocess: preprocess(),
    kit: {
        adapter: adapter({
            pages: '../static',
            assets: '../static',
        }),
        prerender: { default: true }
    }
};
export default config;
更新:
如果我删除HTML文件,我可以得到目录列表,但是所有这些选项都会返回404错误。
英文:
I'm using sveltekit as my frontend for an app I'm working on. I've chosen a static adapter in the svelte.config.js which outputs all necessary components as static files.
Here is the structure of my static directory.

The upper most files load, however, files in static/_app do not
func main() {
	router := mux.NewRouter()
	router.Handle("/", http.FileServer(http.Dir("static")))
	router.HandleFunc("/large-files", largeFilesHandler).Methods("OPTIONS", "POST")
	router.HandleFunc("/delete-file", deleteFileHandler).Methods("DELETE", "OPTIONS")
	logrus.SetLevel(logrus.ErrorLevel)
	err := http.ListenAndServe(":8000", router)
	if err != nil {
		logrus.Fatal(err)
	}
}
And in my svelte.config.js
import adapter from '@sveltejs/adapter-static';
import preprocess from 'svelte-preprocess';
/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://github.com/sveltejs/svelte-preprocess
	// for more information about preprocessors
	preprocess: preprocess(),
	kit: {
		adapter: adapter({
			pages: '../static',
			assets: '../static',
		})
		,
		prerender: { default: true }
	}
};
export default config;
Update:
If I remove the html files, I get the directory list, but all of these options fail with a 404
答案1
得分: 2
mux以不同的方式处理静态文件。
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static")))
英文:
mux handles static files differently.
	router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static")))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论