0

Trying to publish a login page using "github.com/thedevsaddam/renderer" package renderer . Not able to call the .js file from inside the template. When tried inlining javascript it worked fine, but not able to load the .js file.

my file structure is

Project
|
+-main.go
|
+-handlers
| |
| +- routes.go
| |
| +- login.go
+-views
| |
| +- _login.html
| +- login.js

main.go

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/gorilla/mux"
    "github.com/higuestssg/handlers"
)

func main() {
    router := mux.NewRouter().StrictSlash(true)

    // This will serve files under http://localhost:8000/static/<filename>
    router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("views/"))))

    portalRouter := router.PathPrefix("/portal").Subrouter()
    handlers.HandleRoutes(portalRouter)
    fmt.Println("listening at localhost:10001")
    log.Fatal(http.ListenAndServe(":10001", router))
}

routes.go

package handlers

import (
  //"net/http"

  "github.com/gorilla/mux"
)

func HandleRoutes(r *mux.Router){
  r.HandleFunc("/login", loginHandler)
  r.HandleFunc("/healthTest", healthTestHandler)
}

login.go

package handlers

import (
    "fmt"
    "net/http"

    //"github.com/gorilla/mux"
    "github.com/thedevsaddam/renderer"
)

var rnd *renderer.Render

func init() {
    opts := renderer.Options{
        ParseGlobPattern: "./views/*.html",
    }
    rnd = renderer.New(opts)
}

// loginHandler renders login page
func loginHandler(w http.ResponseWriter, r *http.Request) {

    data := struct {
        Val1 string
        Val2 string
    }{
        "test100",
        "test2",
    }
    fmt.Println("login page hit")
    rnd.HTML(w, http.StatusOK, "_login", data)
}

_login.html

{{ define "_login" }}
<!--
https://medium.com/@thedevsaddam/easy-way-to-render-html-in-go-34575f858026
-->
<!DOCTYPE html>
<html lang="en">
<!-- Bootstrap import CSS-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <body>
    <div class="container">
      <div class="starter-template jumbotron text-center">
        <h1>HI GUEST</h1>
        <div class="col-sm-4">
        <p class="lead">Welcome to <strong>HI_GUEST --ssg</strong> page</p>
        </div>
      </div>
    </div><!-- /.container -->
    <div class="row">
      <div class="col-sm-4"><p class="lead" id="idtest1">Test1</p></div>
      <div class="col-sm-4"><p class="lead">Test2</p></div>
      <div class="col-sm-4"><p class="lead"><button type="button" class="btn btn-info btn-lg" name="btn_ip" id="btn_id" onclick="myFunction()">{{.Val1}}</button></p></div>
    </div>
  </body>
  <script type='text/javascript' src='login.js'></script>
</html>
{{ end }}

login.js


function myFunction() 
{
 alert("Hello");
}

$(document).ready(function () {
  alert("Hello");
});

When checked in chrome using viewSource, when clicked on login.js its showing 404 not found

8
  • How are you serving the assets? Show the code that sets up your server router. Commented Jun 21, 2019 at 9:34
  • Please put that into the question by editing it, not the comment. Commented Jun 21, 2019 at 9:39
  • 2
    updated the question.. please check.. Commented Jun 21, 2019 at 9:42
  • Right, you're missing the code that sets up your router to serve static files, like login.js. Go to gorilla mux's github project page, the README contains examples on how to serve static files if I'm not mistaken. Commented Jun 21, 2019 at 9:43
  • 1
    yes now its working fine. Thank you @mkopriva Commented Jun 21, 2019 at 10:14

1 Answer 1

1

updated _login.html , its working fine with @mkopriva suggestion

{{ define "_login" }}
<!--
https://medium.com/@thedevsaddam/easy-way-to-render-html-in-go-34575f858026
-->
<!DOCTYPE html>
<html lang="en">
<!-- Bootstrap import CSS-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <body>
    <div class="container">
      <div class="starter-template jumbotron text-center">
        <h1>HI GUEST</h1>
        <div class="col-sm-4">
        <p class="lead">Welcome to <strong>HI_GUEST --ssg</strong> page</p>
        </div>
      </div>
    </div><!-- /.container -->
    <div class="row">
      <div class="col-sm-4"><p class="lead" id="idtest1">Test1</p></div>
      <div class="col-sm-4"><p class="lead">Test2</p></div>
      <div class="col-sm-4"><p class="lead"><button type="button" class="btn btn-info btn-lg" name="btn_ip" id="btn_id" onclick="myFunction()">{{.Val1}}</button></p></div>
    </div>
  </body>
  <script type='text/javascript' src='/static/login.js'></script>
</html>
{{ end }}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.