1
--public/javascripts/myscript.js

--views/index.ejs

No matter what I do, or what variation I use (ie declaring including type="text/javascript") it wont find my external js file. I am using node js and I was told I may have to do something with routing, but that doesn't seem to be working either.

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
   <!-- <link rel='stylesheet' href='/stylesheets/style.css' />-->

  </head>
  <body>

  <%include templates/header.ejs %>

  <script type="text/javascript" src=".../public/javascripts/myscript.js"> </script>

  </body>
</html>

I understand using routing in other .js files but not importing into html.

Also is there a better/neater was of doing this?

1 Answer 1

1

you base directory for adding static file, start from public
ex.
your project directory
/var/www/project/
your assets located on
/var/www/project/public/javascripts
then you attach theme as bellow

//app.js
app.use(express.static(path.join(__dirname, 'public')));

and on views you can do as bellow

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

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.