Depending on incoming client GET request, I return prepared HTML sites based on the res.sendfile
instruction. So far everything works fine.
But now, I introdced an extended GET route, featuring a URL encoded parameter (verificationcode).
app.get('/validateuser/:verificationcode/', function(req, res){
res.sendfile(__dirname+"/public/valiuser.html");
});
Looking at the served HTML site (valiuser.html
) in the browser, it appears that all paths to linked external resources (images, css files, javascript files, ...) are lost. Once I change the route to:
app.get('/validateuser', function(req, res){
res.sendfile(__dirname+"/public/valiuser.html");
});
Everything works all fine. Why is this additional GET parameter messing up my paths?
The code above is located in my server.js file. On the same level, I have a public folder containing the html files and other "external resources".
Here is an excerpt from valiuser.html:
<html>
<head>
<title>my page</title>
<link rel="stylesheet" href="public/css/general.css">
<script src="public/js/top.js"></script>
</head>
<body>
<img src="public/images/logo.jpg">
</body>
</html>
Thanks, Igor
Your paths are relative to path of your current page.
To make your life easier you may want to use express.static
to serve up your static files (If you have not already).
For your last route use:
app.use(express.static(path.join(__dirname, 'public')));
This will catch any route not caught by your other routes and see if a static file exists in your public directory.
Then in your valiuser.html change your paths:
<html>
<head>
<title>my page</title>
<link rel="stylesheet" href="/css/general.css">
<script src="/js/top.js"></script>
</head>
<body>
<img src="/images/logo.jpg">
</body>
</html>