Reputation: 45
I'm facing a problem with paths in nodeJs, I route the user to the index page when he specifies the language in the url like this :
app.all('/:locale?/index',function(req,res,next){
if(!req.params.locale){
console.log('no param');
res.render('index');
} else {
var language = req.params.locale;
i18n.setLocale(language);
res.render('index');
}
});
However, in my index.html page the source of images are specified this way : ./images/img1.png , when I route the user, my index.html shows image not found because it considers the path " lang/images/img1.png , it considers the languge in my URL, could you please help?
Thank you
Upvotes: 3
Views: 144
Reputation: 1287
The .
in your path is telling the app to look at the current folder, which is lang
. You should be able to get around this by specifying either a URL:
<img src="http://myApp.com/images/img1.png">
or by specifying the path from the root directory (everything except http://myApp.com
)
<img src="/images/img1.png">
This is probably a better solution, since you can swap your domain easily; for example, working on your local machine (http://localhost:3000/
) vs. a deployed app (http://myApp.com
)
Generally speaking, I'd almost always use the path from root rather than a relative path (e.g., ./...
), since I may move pages around in refactoring, and it's easier to look for direct references than relative ones if I have to change anything.
Upvotes: 2