1

I have a problem where my javascript file is not loading. Here is my file directory:

app/views/methods.js app/views/methods.handlebars

I want to load the methods.js file in the methods.handlebars page:

<script type="text/javascript" src="./views/methods.js"></script>
<div class="container">
  <div class="form-group">
    <label>First Number:</label>
    <input type="text" name="firstNum">
  </div>
  <div class="form-group">
    <label>Second Number:</label>
    <input type="text" name="secondNum">
  </div>
  <button type="" name="calculate" class="btn btn-default">Calculate</button>
</div>

However, I keep getting a cannot GET error in the console... can someone help?

Thanks!

2
  • keep methods.js in public directory and do src="/methods.js" or src="domain.com/methods.js" because after html file is sent to client, it vl try to fetch script methods.js and in views directory it's not publically accessible Commented Nov 21, 2015 at 20:01
  • Have you configures express static to serve static files? Commented Nov 21, 2015 at 20:03

2 Answers 2

2

I think your problem might be related to it being in the Views directory.

Try creating a directory for scripts in the public directory.

So assuming you create a javascript directory that would make your call to get the script as

<script type="text/javascript" src="javascript/methods.js"></script> 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi - I did the following: - added app.use(express.static('public')); to my server.js file - added a app/public/javascript directory and placed my methods.js file in it - changed my script declaration to <script type="text/javascript" src="/javascript/methods.js"></script> but I'm still getting the same issue.. am I missing something?
0

I'm a total noob at this - i forgot that I need to reset the node server whenever i make server-side code changes. What I have totally works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.