Ro. Ro. - 1 year ago 93
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

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


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: });

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download