Kshri
Kshri

Reputation: 414

Not able to load the static files from express.js

I am new to node.js and back-end programming. I am not able to load the static files from express. I am not using the 'public' directory. I need the js files from 'js' folder. This is the folder structure folder structure:

In index.js I have mentioned: app.use('/js',express.static(__dirname + '/js'));

index.html:
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<script type="text/javascript" src="js/sngulsr.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.1.3/ui-bootstrap.min.js"></script>
<script type="text/javascript" src="js/angular-routing.js"></script>
<script type="text/javascript" src="js/angular-main.js"></script>
<script type="text/javascript" src="js/controllers.js"></script>
<script type="text/javascript" src="js/custom.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.js"></script>

(these are the files which are not getting loaded)

So, when I run the file, I get $ not defined error since jquery is not getting loaded.

EDIT: After removing __dirname from app.use imageEDITED_IMG

Upvotes: 2

Views: 801

Answers (1)

Shan Robertson
Shan Robertson

Reputation: 2742

Modify your config slightly

app.use(express.static(__dirname + '/js'));

and then reference all js paths relative to the js folder, for example.

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

The path you define in the static method becomes the root for your static files.

Upvotes: 1

Related Questions