go


How do I serve CSS and JS in Go Lang


I followed the GoLang tutorial here but for whatever reason I am having trouble getting the app to serve css and js. If I run my static page without the go server the page css works fine. When I run the go server on the other hand the css just doesn't work.
Here is what my html sort of looks like:
<link rel="stylesheet" href="../assets/css/bootstrap.min.css">
<link rel="stylesheet" href="../assets/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../assets/css/custom.css">
.
.
.
then under the body tag
<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
.
.
.
My file tree looks like this
How do I get my go application to serve the css and javascript I need?
The problem has since been solved, here is the working main:
func main() {
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/index/", makeHandler(indexHandler))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
Here is an example of the handlers I am using
func indexHandler(w http.ResponseWriter, r *http.Request, title string) {
p := &Page{Title: title}
err := templates.ExecuteTemplate(w, "index.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
You can serve a directory of static files like so:
http.Handle("/", http.FileServer(http.Dir("css/")))
Would serve your css directory at /. Of course you can serve whichever directory at whatever path you choose.
I would probably make sure that the static path isn't in the way of other paths and use something like this.
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
Placing both your js and css in the directory static in your project. This would then serve them at domain.com/static/css/filename.css and domain.com/static/js/filename.js
The StripPrefix method removes the prefix, so it doesn't try to search e.g. in the static directory for static/css/filename.css which, of course, it wouldn't find. It would look for css/filename.css in the static directory, which would be correct.

Related Links

Unable to send JSON as body for HTTP POST request in GO
Go run performance
HTTP POST in GO Lang throws error
end input programmatically in a golang terminal application
golang read line by line
Idiomatic way to handle template errors in golang
error libxml2 not found on installing gokogiri on windows
Go Templates: Using methods with multiple return values
Go - loop through map on the same order multiple times
AMQP guaranteed delivery in Go (golang)
Bosun adding external collectors
Golang https server passing certFile and kyeFile in terms of byte array
multiline input in a terminal Go application
In html/templates is there any way to have a constant header/footer across all pages?
How to get socket.io handshake query in Go
Go float64 does not work for latitude and longitude

Categories

HOME
batch-file
microsoftgraph
jpa
telegram
jmx
sublimetext
javacc
hugo
android-json
data-visualization
jax-ws
computer-science
java.util.logging
alamofire
datastage
favicon
zerobrane
scrolltop
bitnami
parameter-passing
gruntfile
azure-powershell
digital-signature
symfony-console
sar
babel
qliksense
epsilon
direct3d11
onload
maze
syntax-highlighting
android-sharedpreferences
jackson-modules
iis-express
dotspatial
xbox
livelink
mockjax
sendbird
vue-chartjs
pdw-file-browser
imx6
video-capture
openerp-7
graphhopper
nohup
bc
branching-and-merging
print-css
signal-strength
libgphoto2
netbeans6.8
tinymce-3
payara-micro
spring-data-couchbase
filehandle
arules
mouseleave
offline.js
sandcastle
chromium-os
pytables
custom-lists
winusb
pacemaker
google-cloud-console
vulcanize
respondcms
pre-build-event
jtwig
angular-ui-typeahead
fink
nupic
design-principles
ascii-art
atan2
custom-errors
file-move
microsoft-reporting
botan
dnssec
snackbar
grouping-sets
verold
parallel-testing
matrix-inverse
android-framework
terminate
spring-3
isqlquery
scmmanager
booksleeve
idn
tree-balancing
os.system
correlated-subquery
node-redis
promotion-code
umra
xui
cdonts
cloudfiles
mtu
photolibrary
message-passing
echo3
build-environment
wysiwym
pnrp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App