How to tell gulp-connect to use a specific base URL? - javascript

Can I tell gulp-connect to use a specific base URL that does not relate to the folder structure it is serving from?
I am trying to serve the app directly from the src folder and therefor set gulp-connect's root to 'src'. The src folder contains the index.html.
When I browse to "localhost:< port >". It shows the index.html, but because of internal use of a JavaScriptvariable set to the production base URL, the internal routing and script loading does not work. That's why I want to tell gulp-connect, to behave as If the production base URL would be left out.
For example: Browsing to 'localhost:8080/MyApp/index.html' should basically do the same as browsing to 'localhost:8080/index.html'.

Related

How to store data with nodejs outside of the project folder?

I am using ExpressJs and i want to save my files outside of the project like this.
im using this code for upload and its ok
my files uploaded correctly but i can not show them in the app cause route will be like this
http://localhost:8000/../../fileArchive/1661839542935/name.jpg
nut when i set this to src of an image, the src doesnt show my image
This is because express is trying to protect your project files from getting exposed on a malicious request. By default you cant access any of the files in your project or other directories. You can define a directory that can be accessed using the express.static method.
This will define a directory as a static directory which can be accessed (see express.static). After you added this you can also drop the any path in your URL. Express will check for any files matching the name in one of your static directories.
// This would allow the user to access the files in that directory
// ../fileArchive will be considered as the fileRoot e.g. localhost:8000/test.txt would look for a test.txt file in ../fileArchive
app.use(express.static('../fileArchive'));
// if you want to keep fileArchive as part of the url you can do that like so
// however I would recommend to exclude the `..` to keep the url readable and also the exact file path on the server should not concern the user just where he can find it
app.use('/fileArchive', express.static('../fileArchive'))

How to avoid using "../" when webpage is located in subdirectory?

I am working on a static website as a web design project for college. It's a tech blog that contain mock articles. I'm storing the website in the root (these are pages like index.htm, news.html, reviews.html, etc). However, I want to avoid clutter for actual article pages. So I store these pages in a subdirectory called articles, example: "articles/article1.html".
Now I have to write two javascript files because of this. My two javascript files would do exactly the same thing except javascript file A is for all pages in the root and javascript file B is for all pages in the articles subdirectory. And that's because my javascript code accesses files in my website. That means I have to prepend "../" to all urls in js file B for my js to work. File A would not have "../" appended.
What can I do to only use 1 javascript file for my static website?
You can prepend the URLs with /. This always take you to the root, so regardless of where the file is, you start the path with the root folder.
Like
img.src = "/Images/img1.jpg";
NOTE
This will not work if your site is running in a subfolder

Javascript is unable to resolve correct path of image when a page is requested using URL Routing in ASP.Net

In my ASP.Net project, I have implemented URL Routing. So even if my page is in the root directory, I am accessing it using URL routing as given below:
My actual Page is projectpage.aspx, which is in the root directory. I have registered its route as project/{projectname}/{cityname}/{projectid}.
There is another javascript which is included in this page. In this javascript, there is a code to access images in "resources" folder which is done by simply resources/{imagename}, but when I do View Source of the page, the path is shown like project/{projectname}/{cityname}/{projectid}/resources/{imagename}. But the "resources" folder is also in the root folder.
I also tried using "~/" before resources folder, but same thing happens.
Please let me know in case anyone has a solution to this problem.
Thanks & Regards,
Munjal
it think you should try /resources/{imagename}

redirecting site to mobile...file name?

This is my first time creating a mobile site for someone I have the javascript code all set up and when tested on my phone, it redirects it to: m.websitename.com, like I want it to. However, I have no idea what to name the file I am wanting it to redirect to? I saved the file under: mobile.html, but it isn't redirecting to it.
I don't want it to redirect saying www.websitename.com/mobile, I don't think it looks as professional.
Thanks
index.html or default.html is the convention. so why not direct to
m.websitename.com/index.html
and configure the subdomain so that m.websitename.com is actually pointing to websitename.com/m folder. so that folder can have its own index file.
You would probably want to do this using subdomain and destination setup from your server you are using (Most of webhosting companies made this easy through their software e.g. cPanel). Directory structures is going to look like this. So when user was redirected t m.yourweb.com/ then it's not going to have m.yourweb.com/mobie.
domain [directory]
css [directory]
js [directory]
index.jsp [file]
purchase.jsp [file]
...
subdomain [directory] <- for mobile
css [directory]
js [directory]
index.jsp [file]
purchase.jsp [file]
So when mobile user was detected, JS is going to redirect this user to the subdomain (for mobile) instead of normal directory.
The other recommendation (which is better I think) is use the same logic from the above but when user was redirected to m.yourweb.com/ load css file for mobile version web instead of normal web. loading css through JS
If you think this is complicated, you might be interested in having a look at CSS3 media queries. This basically changes CSS layout depending on current users' screen. resolution Media queries tutorial
You can have any filename you like. But as btevfik mentioned either put index.html or default.html
I recommend index.html
Also you can set the default file in you server configuration. By default most servers have the default file handler as index.html. For Apache it will be either index.html or index.php. You can change this to something like mobile-handler.html by changing the server configuration.
So whenever you access your site by http://your-site.com or m.your-site.com the default file specified in the server configuration is loaded.
Please note that your-site.com and m.your-site.com will have different site root, hence request will be handled by difffernt files.

JS Ajax: local vs absolute path issue when using SEO friendly urls

here's the thing:
i built my site with SEO friendly urls...but i have problem now calling ajax files becaus eth epath is wrong and I cant set an absolute url when i call my files in background..
for instance my page is here (similar to StackOverflow..)
www.domain.com/product/123/this-is-a-product
but my javascripts functions, in folder /js,now they try to reach the files but they cant obvisouly because are set to relative path...
how can i solve this issue??
EDIT: Found this How to get the root path in JavaScript?
When you are using freindly urls, then you have to use path started with /. But then you are starting path from main path.

Categories

Resources