2

I've got a Node/Express/EJS app. It's got a folder for views and another for client files. The latter has another folder for javascript, where I have a file called frontend.js.

I'd like to load jQuery and the frontend.js file in this view.

The standard script tags don't work.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="../client/js/frontend.js"></script>

jQuery loads, frontend.js throws a 404 error.

Is there a special way of doing this that I'm not aware of?

1 Answer 1

10

in your app.js file add this :

app.use(express.static(path.join(__dirname, 'client')))

change the second script tag to :

<script type="text/javascript" src="/js/frontend.js"></script>

I'm assuming that your app.js file and client folder are in the same folder.

Sign up to request clarification or add additional context in comments.

2 Comments

I use the function in frontend.js <% myfunc("strParam") %> but no luck :(
No need for the path.join

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.