Ro. Ro. - 2 months ago 10
Node.js Question

Expressjs Adding :id param to url brokes static content

I return same view for both /new and edit/:id

however when it is rendered styles and js files are not found for
edit/:id


Why is this happening this for Expressjs is not supposed to always be relative to public folder

Routes:

router.get('/new', function(req, res, next) {
res.render('index', { title: 'New', id: 0 });
});

router.get('/edit/:id', function(req, res, next) {
res.render('index', { title: 'Edit', id: req.params.id });
});


On the view i have my styles like this

<script src="javascripts/jquery-3.1.0.min.js"></script>
<link href="bootstrap-3.3.7/css/bootstrap.min.css" rel="stylesheet">


All content is inside public folder

Answer

You need absolute urls for your scripts and links (start with a slash).

<script src="/javascripts/jquery-3.1.0.min.js"></script>
<link href="/bootstrap-3.3.7/css/bootstrap.min.css" rel="stylesheet">

Your browser sees a relative path, so it is requesting the content at

edit/javascript/jquery-3.1.0.min.js which doesn't exist.