-1

I want to remove the last trailing slash in the path, ex: localhost:8080/Login/ --> localhost:8080/Login. In code, I use /login instead of /login/ but css and js don't work, please help!

This is my code:

Go file:

func main() {
    fileServer := http.FileServer(http.Dir("pages"))
    //http.Handle("/Login/", http.StripPrefix("/Login/", fileServer))
    http.Handle("/Login", http.StripPrefix("/Login", fileServer))

    http.ListenAndServe(":8080", nil)
}

HTTP file:

<link href="/css/bootstrap/bootstrap-4.3.1.css" rel="stylesheet" />
<script src="/js/bootstrap/bootstrap-4.3.1.js"></script>
  • do you have 'js' and 'css' folders inside your 'pages' folder? – user10753492 Oct 29 '19 at 04:33
  • I store 'js' and 'css' folders inside the 'pages' folder, if I use '/login/', it works, but not for '/login' – Sang Nguyen Trong Oct 29 '19 at 05:41
  • update your code. `http.Handle("/Login/", http.StripPrefix("/Login", fileServer))`. reference: https://stackoverflow.com/questions/28793619/golang-what-to-use-http-servefile-or-http-fileserver – sh.seo Oct 29 '19 at 06:02
  • It works, but on the web browser, when I type **localhost:8080/Login** and press Enter, the path will automatically change to **localhost:8080/Login/**, I want to remove the last slash, the url must be **localhost:8080/Login** – Sang Nguyen Trong Oct 29 '19 at 06:25
  • This is how the default muxer works and cannot be changed without using a different muxer. – Volker Oct 29 '19 at 07:42
  • 1
    I think you don't really understand what's going on. It's hard to see how the url changing has anything to do with js and css files not loading. – user10753492 Oct 29 '19 at 07:42
  • 2
    @Volker, registering the same handler for /x/ and /x disables the redirect. https://golang.org/pkg/net/http/#ServeMux – Peter Oct 29 '19 at 08:00
  • @Peter Neat trick. Thanks! – Volker Oct 29 '19 at 08:41

3 Answers3

1

user10753492 is correct. Because you're using rooted paths for the CSS and JS files the trailing slash doesn't matter as far as the requests for the CSS and JS files are concerned.

As for the redirect to /Login/: this behavior is documented along with instructions on how to prevent that:

If a subtree has been registered and a request is received naming the subtree root without its trailing slash, ServeMux redirects that request to the subtree root (adding the trailing slash). This behavior can be overridden with a separate registration for the path without the trailing slash.

So if you don't want the redirect, register the same handler for both /Login and /Login/.

Peter
  • 29,454
  • 5
  • 48
  • 60
0

You are basically mounting your frontend at "/Login".

But your js and css are being requested from "/"

So of course they won't be loaded.

You have to either:

  1. Change your css and js references to be "/Login/js/...." and "/Login/css/..."

    This doesn't seem like what you actually want to do though

  2. Or, mount the frontend at "/"

    http.Handle("/", fileServer)
    
user10753492
  • 720
  • 4
  • 8
-3
package main
import (
    "fmt"
)
func main() {
  var s string = "localhost:8080/Login/"
  fmt.Println(s)
  for _,i := range s{
    fmt.Println("hi,test data ->",string(i))
  }
  fmt.Println("This string len is ",len(s))
  fmt.Println("You want ",string(s[0:len(s)-1]))
}
sober
  • 37
  • 3