• Download
  • Contact
  • Terms of Service
  • Privacy Policy
  • About US
Codershood
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others
No Result
View All Result
Codershood
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others
No Result
View All Result
Codershood
No Result
View All Result

Serving Static Files in GoLang using Gorilla Mux

by Shashank Tiwari
February 16, 2020
in GO lang
0
< 1 minute
Serving Static Files in GoLang using Gorilla Mux

In this article, we will understand how Serving Static Files in GoLang is very easy with the help of Gorilla Mux. Using Gorilla Mux and the HTTP module in GoLang, we can serve static files directly from the file system.

http.FileServeris the Handler that we can use to serve static files to the user. FileServer returns a handler that serves HTTP requests with the contents of the file system rooted at the root.

 




 

Here is the simple example code:

package main

import (
    "log"
    "net/http"

    "github.com/gorilla/mux"
)

func main() {

    log.Println("Server will start at http://localhost:8000/")

    route := mux.NewRouter()

    fs := http.FileServer(http.Dir("./public/"))
    route.PathPrefix("/public/").Handler(http.StripPrefix("/public/", fs))

    log.Fatal(http.ListenAndServe(":8000", route))
}

Assuming you have files stored in the public folder, you can now make the request to the files from the browser.

For example, if the file stored isscript.jsin the public folder, then request URL would look like this:

http://localhost:8000/public/script.js

If there is a folder inside the public folder, you need to include that in the path.

http://localhost:8000/files/js/script.js

You can also specify the static paths in the custom middleware. For example, if you want the files to serve by the /files route. You can do so by using the following code.

package main

import (
    "log"
    "net/http"

    "github.com/gorilla/mux"
)

func main() {

    log.Println("Server will start at http://localhost:8000/")

    route := mux.NewRouter()

    fs := http.FileServer(http.Dir("./public/"))
    route.PathPrefix("/files/").Handler(http.StripPrefix("/files/", fs))

    log.Fatal(http.ListenAndServe(":8000", route))
}

I do recommend that you use a reverse proxy or third-party CDN to serve static files in a production environment instead of doing it in Express.

Checkout more GoLang tutorials.

Tags: File ServerGoGo File ServerGo serverGolangGolang serverStatic Server
Previous Post

Golang and MongoDB connection tutorial

Next Post

Send a JSON response in golang using Gorilla mux

Related Posts

Real time private chatting app using React, Golang and mongodb banner-part 2
GO lang

Real time private chatting app using GoLang, React and mongodb – Part 2

July 4, 2020
Real time private chatting app using React, Golang and mongodb banner
GO lang

Real time private chatting app using GoLang, React and Mongodb – Part 1

July 4, 2020
Sending message to specific user with GoLang WebSocket
GO lang

Sending message to specific user with GoLang WebSocket

August 6, 2023
Next Post
Send a JSON response in golang using Gorilla mux

Send a JSON response in golang using Gorilla mux

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *




https://codershood.info

www.codershood.info programming blog dedicated to providing high-quality coding tutorial and articles on web development, Angular, React, Laravel, AngularJs, CSS, Node.js, ExpressJs and many more. We also provide ebook based on complicated web application along with the source code.

  • Download
  • Contact
  • Terms of Service
  • Privacy Policy
  • About US

www.codershood.info is licensed under a Creative Commons Attribution 4.0 International License.

No Result
View All Result
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others

www.codershood.info is licensed under a Creative Commons Attribution 4.0 International License.