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
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.
Related
I have a simple use case for an application; a user can upload images to server where they are stored in a directory, and a user can view all images uploaded and stored in this directory.
The functionality for the first criteria is working, and the application persists uploaded images to the image directory.
The next step is to return all images. For the front end, i will use some kind of tile gallery to render images to the user. However, my question is related to how best to return "all images" to the end user, through express js.
Essentially, I am hoping to do a directory dump, but am confused as to an elegant (read: any!) solution on how to do this. Any advice or suggestion most appreciated.
Edit: to clarify, the route should return all images when hit. As future implementations will filter images based on certain criteria.
example:
// return the view page for register
app.get('/gallery',function(req,res){
// return images here
res.send();
});
If I understood you correctly, all you need to do is to statically serve a folder. For that, you can use
app.use('/images', express.static('path/to/images'))
I am using Responsive FileManager extensively as a standalone (not part of a general text editor). I sometimes call it multiple times on one page. However, each time I want it to be restricted to a certain folder—not merely so it defaults to a folder, but so the user can't navigate to any other folders at all.
The only way I have found to restrict a folder in Responsive FileManager is by setting the PHP sesssion variable:
$_SESSION['RF']['subfolder']
However, this is a problem for different folders that need to be there on the same page, and in general it's impractical because the restriction should be specific to the file manager call, not user-specific like a session. Also, I don't need it to be absolutely impossible to navigate to other folders in a secure way, it's OK if someone who knows how to use the browser debugger navigates somewhere else. I merely want to prevent casual users from doing this.
Is there any way to do this with a JS config option, or some other per-call and not per-user way?
Their feature list says:
You can set a subfolder as the root and change the configuration for each user, page or FileManager call.
But I was not able to find a per-call folder restriction in their docs.
Here is a simple tweak to achieve this .
Responisve filemanager calls the dialog.php with some parameters appended like type .What I did is : introduce a new param and put a check for that on the server side to set the uploads path dynamically .
Consider your uploads directory is uploads
and you have user1 , user2 , user3 as sub directories , So , in that case we will be setting the uploads path and current path in the config.php dynamically from the parameters which we will be passing while calling the dialog.php
In the filemanager/config/config.php
Append these lines
if( isset( $_GET['MY_UPLOAD_PATH']))
{
$config['upload_dir'] = $config['upload_dir'] . $_GET['MY_UPLOAD_PATH']."/" ;
$config['current_path'] = $config['current_path'] . $_GET['MY_UPLOAD_PATH']."/" ;
}
In the filemanager/dialog.php
Find this line
$get_params = http_build_query($get_params);
And just before that line add
if(isset($_GET['MY_UPLOAD_PATH'])){
$get_params['MY_UPLOAD_PATH'] = $_GET['MY_UPLOAD_PATH'] ;
}
Now change your variable
$data['file_explorer'] = base_url('assets/resources/filemanager/dialog.php?type=0');
to
$data['file_explorer'] = base_url('assets/resources/filemanager/dialog.php?type=0&MY_UPLOAD_PATH=user1');
Just change the value of the MY_UPLOAD_PATH param and the dialog.php will show only that particular directory .
In my express.js project, I am serving up the dist folder of my project like so:
server.use(express.static('dist'))
In my dist folder, I have an img/fileicons folder that contains PNG files that represent various file types, e.g. 'txt.png', 'html.png', 'pdf.png', etc. In case an image doesn't exist for a given file type, I want to set up a default route that would serve up 'blank.png' with a generic file image. For example, if the URL '/img/fileicons/[doesn't exist].png' is hit, then 'blank.png' is returned. However, if I navigate to another path that doesn't exist, like '/html/[doesn't exist].html', then I don't want the 'blank.png' file served up.
How can I set up my express.js routing to accommodate this need?
At the bottom of the routes, make a default one:
app.get('/dist/img/fileicons/*', function(req, res) {
res.sendfile('path/to/blank.png')
});
All you need to do is put some middleware at the bottom of your stack and use res.sendfile(). Don't forget to use res.statusCode(404) so that crawlers don't think they're hitting a real resource.
I have following url to be build,
http://localhost/myweb/cart/index.php
I want to get the http://localhost/myweb/ bit build dynamically.
To do that on my live web site which is http://www.myweb.com/cart/index.php I can use the following JavaScript code,
var http = location.protocol;
var slashes = http.concat("//");
var host = slashes.concat(window.location.hostname);
But how do I get my development environment to work since it has http://localhost/myweb/? If I run the above code it will give me http://localhost/ only.
Any suggestions?
window.location.pathname is the thing you search for.
I would suggest you to read the MDN description of window.location. Like everything else in MDN, this is also really straightforward and informative.
If you know that the URL has an unnecessary index.html part at the end you can:
var path = window.location.pathname.split('/');
path.pop();
path.join('/');
or you can slice it (since it is generally faster):
path.slice(0,path.lastIndexOf('/')+1)
EDIT:
Seeing your new question I can say that what you want can't be done consistently and safely by only the current URL.
You need the http://localhost/myweb/ part, which is the URL root of your application. In javascript you are able to get the protocol and domain of the url. On your live site these 2 match, but if your application resides in a subfolder (like the myweb folder at your localhost), this will fail.
What you need is to somehow identify the application URL (the URL root of your application).
The problem is that by only examining the URL, javascript cannot tell where your application resides.
Let's say you deploy your site to: http://localhost/myweb/site1/
You will have the following URL: http://localhost/myweb/site1/cart/index.php
Javascript can split your URL by the slashes (/) but it has no way of nowing how many subfolders it should select. For example from the URL above your application root can be any of the following: http://localhost/, http://localhost/myweb/, http://localhost/myweb/site1/, http://localhost/myweb/site1/cart/.
By an other approach (which I suggested first) you can drop the end of the URL (in your case the cart/index.php part). This will only work if your URL structure IS very rigid, so all the pages this script is executed on reside in one subfolder.
So it will break on the following URL: http://localhost/myweb/site1/gallery/old/index.php or similar.
Your best bet would be to make this a "config variable" in a separate file which you edit on every location it is deployed to.
Either as a PHP variable ($appRoot = "http://localhost/myweb/") which you generate the javascript with.
Or more simply a javascript variable (var appRoot = 'http://localhost/myweb/'). Make a separate js file, call it something like config.js, add the above line to it and reference it before your other javascripts.
I have this site that I need to find the root folder / plus the actual folder its works out of.
My problem here is that during development i have the folder with in my local server that in turn is with in its own folder:
Then online I then have the development site within a folder, so it can all be tested before the live production etc.
LOCAL SERVER:
localhost/mytestSiteA/...
LIVE SERVER TEST FOLDER:
www.asite.com/devbuild/....
Now I can retrieve the root via the
document.location.hostname
But i need then to add the folder name after this so that I can load in content etc when in developement mode.
LOCAL SERVER
document.location.hostname + '/mytestSiteA/'
LIVE TEST SITE
document.location.hostname + '/devbuild/'
But my issue is, is there an easy way to gain this inner folder rather than setting up variables determined on whether in local dev, live dev or live mode, as can be a pain, and would be nice to gain the current inner folder dynamically rather that manually changing etc so that I can add my paths correctly.
Also would help as if I have a folder within this that also loads in js script it can obtain its full path.
LOCAL SERVER:
localhost/mytestSiteA/subsection/...
LIVE SERVER TEST FOLDER:
www.asite.com/devbuild/subsection/...
I hope I have made this as easy to understand and put across.
Si
try to switch
switch (document.location.hostname)
{
case 'asite.com':
var rootFolder = '/devbuild/'; break;
case 'localhost' :
var rootFolder = '/mytestSiteA/'; break;
default : // set whatever you want
}
and then use
var root = document.location.hostname + rootFolder;
This is what worked for me after the switch clause.
var root = location.protocol + '//' + location.host + rootFolder;
if someone needs to move a step back
location.href.slice(0,location.href.lastIndexOf("/"))
You can map the url localhost/devbuild to localhost/mytestSiteA and use the first url to test your site locally. In your javascript you can always assume the devbuild folder then. That way you don't have to change anything else.
use relative paths so you don't need to get to the root folder
this doesn't work on sites with friendly urls with folders in the links