Coder1000
Coder1000

Reputation: 4461

How to load local scripts on the front-end?

CODE:

app.js

//Static Folder
app.use(express.static(path.join(__dirname, "/public")));

section1/index.ejs

<script src="/public/js/firebase.js"></script>
<script src="/public/js/angular.js"></script>
<script src="/public/js/angularfire.js"></script>
<script src="/public/js/jquery-3.1.1.min.js"></script>

PROBLEM:

Error 404 for all those files when I load section1/index.ejs

What is missing ? What mistake have I made ?

Upvotes: 1

Views: 149

Answers (2)

user6531368
user6531368

Reputation:

Try using:

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

And I think if you want to use it as a middleware without a virtual path, you should use it like this:

app.use(express.static('public'));

Without defining the whole path, Express will get the folder for you automatically.

Upvotes: 0

Fabian Schultz
Fabian Schultz

Reputation: 18546

You need to pass a first argument to app.use, which will be the public path.

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

Then on the client:

<script src="/js/firebase.js"></script>

You can also use /public instead of /.

Upvotes: 4

Related Questions