404 Page Not Found - Go Rendering CSS File

404 page not found - Go rendering css file

Since you don't specify full path for the css folder just a relative one, whether your css files are found depends on the folder you run your application from (the working directory, this is what relative paths are resolved to).

For example if you start your application from your src with go run server/server.go it will work. If you start it from your src/server folder with go run server.go, it will not work. Also if you create a native executable from your app which is put into the bin folder and you start that from the bin folder, this also won't work because the css folder is not in the bin folder.

Either start it with go run server/server.go from the src folder, or copy the css folder to your bin folder and start the executable from the bin folder and it should work (but in this case you also have to copy other static files like html templates).

GET 404 error for style.css not found

404 is a 'not found' error. Meaning, the browser cannot find your style.css with the path it was given. If it is working on some templates and not others, it is because you may have a different file structure for some templates, a sub-folder for example.

A quick fix would be to make your style.css load from an absolute path

https://yourdomain.com/public/css/style.css

A better solution would be to traverse the directory to the css folder and then reference your style.css.

Learn about relative paths and find a solution that works for all templates. The answer is likely to be something like:

/public/css/style.css

However, it could be something like:

../public/css/style.css

Where goes back to one more previous directory. More about absolute vs. relative paths http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/

Go http.Handle() not working as expected. 404 file not found

I'd suggest moving your files like so (note I renamed index.html to lowercase - so it will be loaded by default when visiting the document root URL):

Main.go
static/
static/index.html
static/css/Styles.css

modify index.html to refer to the more aptly named css directory:

<link rel="stylesheet" type="text/css" href="css/Styles.css"/>

EDIT: update to adjust for gorilla/mux.

H/T to this answer.

package main

import (
"github.com/gorilla/mux"
"log"
"net/http"
)

func main() {
r := mux.NewRouter()

r.PathPrefix("/css/").Handler(
http.StripPrefix("/css/", http.FileServer(http.Dir("static/css/"))),
)

err := http.ListenAndServe(":8080", r)
if err != nil {
log.Fatal(err)
}

// curl 'localhost:8080/css/Styles.css'
// <style> ... </style>
}

FileServer handles not found css with MIME type error instead of 404

From the net/http source code (fs.go):

// toHTTPError returns a non-specific HTTP error message and status code
// for a given non-nil error value. It's important that toHTTPError does not
// actually return err.Error(), since msg and httpStatus are returned to users,
// and historically Go's ServeContent always returned just "404 Not Found" for
// all errors. We don't want to start leaking information in error messages.
func toHTTPError(err error) (msg string, httpStatus int) {
if os.IsNotExist(err) {
return "404 page not found", StatusNotFound
}
if os.IsPermission(err) {
return "403 Forbidden", StatusForbidden
}
// Default:
return "500 Internal Server Error", StatusInternalServerError
}

The file server returns a 200 with a plain text file for 404 errors. The browser tries to interpret this plain text error page as a CSS file, and throws the error.

This behavior of returning the plain text file cannot be overridden on the handler returned by FileServer().

As has been pointed out already, this isn't really a bug in net/http.

If this behavior is undesirable for you for some reason, you can explore creating a custom handler for 404 responses, which has been explored in this thread. You could also use a routing library like Gorilla which has overridable behavior for not found pages.

CSS file is not applied to 404 error page when I go to localhost/about/{randomtext} in nodejs

Use app.use(express.static(staticFilePath)) instead of app.use('public', express.static(staticFilePath))

404 error CSS file not found in chrome dev tools

You're missing a handler for the static directory:

handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /static
static_dir: static
- url: .*
script: main.app

Learn more about why this is necessary.

http.FileServer is sending 404 page not found

Does the directory /assets exist? Note that /assets is an absolute path, so it must be at the root of your filesystem. If you want something in the working directory where you're executing your program, you should use ./assets.



Related Topics



Leave a reply



Submit