- Building Microservices with Go
- Nic Jackson
- 190字
- 2021-07-15 17:28:04
Static file handler
Whilst we are mostly going to be dealing with APIs in this book, it is a useful illustration to see how the default router and paths work by adding a secondary endpoint.
As a little exercise, try to modify the code in reading_writing_json_5/reading_writing_json_5.go to add an endpoint /cat, which returns the cat picture specified in the URI. To give you a little hint, you are going to need to use the FileServer function on the net/http package and your URI will look something like http://localhost:8080/cat/cat.jpg.
Did it work the first time or did you forget to add the StripPrefix handler?
Example 1.7 reading_writing_json_6/reading_writing_json_6.go:
21 cathandler := http.FileServer(http.Dir("./images"))
22 http.Handle("/cat/", http.StripPrefix("/cat/", cathandler))
In the preceding example, we are registering a StripPrefix handler with our path /cat/. If we did not do this, then the FileServer handler would be looking for our image in the images/cat directory. It is also worth reminding ourselves about the difference with /cat and /cat/ as paths. If we registered our path as /cat then we would not match /cat/cat.jpg. If we register our path as /cat/, we will match both /cat and /cat/whatever.
- Mastering Ext JS(Second Edition)
- 摩登創客:與智能手機和平板電腦共舞
- Swift細致入門與最佳實踐
- INSTANT Adobe Edge Inspect Starter
- App Inventor少兒趣味編程動手做
- Web程序設計:ASP.NET(第2版)
- 創意UI Photoshop玩轉移動UI設計
- Mastering Android Studio 3
- Web前端開發技術:HTML、CSS、JavaScript
- Hack與HHVM權威指南
- Getting Started with Electronic Projects
- Flutter從0基礎到App上線
- C++面向對象程序設計
- Natural Language Processing with Python Cookbook
- PHP程序設計經典300例