Snappy
Snappy

Reputation: 203

parameter from url adds in static directory path

When I try to res.sendfile(__dirname + '/index.html'); using express, first parameter start adds to the static directory path which then gives not found error.

here is my code.

app.get('/start/me', (req, res) => {
   res.sendfile(__dirname + '/index.html');
});

this is what happens.

GET http://localhost:3000/start/static/jquery.js 404 (Not Found)

But if I remove /me it serves fine.

route for handling statics files:

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

on front-end

<script src="./static/jquery.js"></script>

I couldn't find solution for this problem. Thanks in advance.

Upvotes: 0

Views: 33

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40404

The issue is you're serving jquery from the wrong path.

According to your /static route, jquery.js should be served from:

http://localhost:3000/static/jquery.js

So your script tag should be:

<script src="/static/jquery.js"></script>
<!-- instead of src="./static/jquery.js" -->

When adding ./ your script is not being served from the root, but is relative to the current url. So if you're in /foo/bar/ it will search for jquery.js in /foo/bar/static/jquery.js

Upvotes: 1

Related Questions