Read remote directory with client-side javascript? - javascript

I'd like to scan a directory on my server, using JS, and create a link to the most recent file in the dir.
My server doesn't disallow directory listings; I can see the contents of the folder if I navigate to it.
Is it possible to do this in straight JS?

All Javascript can do is HTTP requests and read whatever your server responds with. If your server doesn't give you any sort of directory listing over HTTP, then there's very little you can do with Javascript. If it does give you a directory listing you could try to parse it, but you still can't create any files or links on the server purely with client-side Javascript. This really calls for a server-side solution (PHP, Python, Ruby, C#, shell scripts; whatever you're comfortable writing).

Related

Where do I put front-end code in my backend project and how/when to run it?

The question is, say I have written a backend REST service using Python, and then some other guy wrote some frontend code in Angular JS. Is the typical workflow putting them into two separate folders and run them separately? So the process will look like below
python manage.py runserver
in Python colder and then probably
# in the angular2 folder
npm start
Or should I place all the JS code into some assets folder and when I start my server, all the JS code will run automatically with them? If so, how should I do it?
Another question related is, when is all the JS code sent to users’ browsers? If the app is a client-side rendering app, is the code sent to browser at the first time of server requesting? How does the server know it should package your JS code and ship it?
Q)The question is, say I have written a backend REST service using Python, and then some other guy wrote some frontend code in Angular JS. Is the typical workflow putting them into two separate folders and run them separately?
Both Angular and Python can be run differently as well as together.
You could choose to place the Angular files (which qualify for all practical purposes as public files) in the public (or related folder) depending on which framework you're using - Django, Flask, Web2py or so on.
You can also choose to run them independently as standalone apps.
Depends on your requirement. Honestly, there are so many ways to answer this question.
Q)Or should I place all the JS code into some assets folder and when I start my server, all the JS code will run automatically with them? If so, how should I do it?
If you place everything in your assets folder, then as soon as the home route or any other route is made a request for [from a browser] , the public folder passes on to the browser.
Although I'm not sure if it is the case with server side rendering.
If you're using Angular 1, I do not think it fits server side rendering although you could use some libraries out there.
Q)Another question related is, when is all the JS code sent to users’ browsers? If the app is a client-side rendering app, is the code sent to browser at the first time of server requesting? How does the server know it should package your JS code and ship it?
All the files in PUBLIC folder on the server are accessible by a browser.
All of your questions seem to essentially ask the same question.
There are many approaches to this problem.
If infrastructure management is difficult for you, than maybe it's easier to place them in the same server. You can create another directory and place your html that is served to browser with your JavaScript code.
In case you have a good pipeline (which I think it pays for it self) than having another server that serves your application is better. You can do more things without affecting your service, like caching etc. And your server that runs your service wont be busy serving assets as well.
I have a case where we run a node server, which serves the html and javascript to our users, because that way we can do server side rendering.enter link description here
The flow of code execution will be this : Once the browser of your user hits the server that is serving html and assets, it will download it locally and it will start with JavaScript execution (Parsing, Compiling and Executing). Your JavaScript app might do some API calls upon initialization or it might not, but your service will be executed only if frontend makes a request.
As for CORS it is irrelevant how you serve them, because you can keep them always in the same domain.

Can I create a directory in client PC?

I have multiple questions
Can we create a directory in client machine ?
Can we check the running browser's default download path?
My requirement is that the user will download a BLOB data from my DB that time I want to create a folder in client machine and use that folder to save the blob data.
Can this be done using javascript,jquery,ajax,php,angularjs ?
You can't create a folder in client PC using JS.(don't even bother looking with HTML and CSS)
The best way you could accomplish something as you describe is to give a .zip to your client, so while extracting it, it creates his own container (the folder). But you must be sure that the people who intend using your web application knows how a .zip works.
Using PHP you can't do anything in the client PC. PHP is a server side programming language. So, it is running only in the server. When you make any request to the server then only PHP performs. In the browser end or client side only HTML, CSS and JS works.

Getting a Python server to execute a script

I'm pretty new to web development, so bear with me if these questions seem pretty fundamental.
My use-case is as follows: I am hosting a server using ngrok which allows me to allow clients to securely tunnel to a server I host locally, using a public http(s) URL that ngrok generates.
Currently the server I am running locally is the simplest possible python web server (I literally just invoke 'python -m SimpleHTTPServer 8080').
Let's say I have a file "simplescript" located in the local directory where I instantiate the Python server. I would like to put in php code in "simplescript" so that when somebody fetches the file by using the browser to access "https://ngrokURL.ngrok.io/simplescript", then my server will execute the php code in the script directly and return the return value (if one is generated) to the client requesting my file.
Right now, when I try to do this, the client accessing my file only receives the actual text content of the script, and so it seems that the script is not being executed at all.
Question:
Is my use-case something that can be achieved using the Python simple HTTPserver, or do I need other tools?
How can I make my scripts executable for my server (if that is something I have to do)?
Thanks for the help!

Reading and writing client files from a web application

I need to implement a mechanism in order for a remote web application to communicate with a local desktop (WinForms) application that I cannot modify nor have the source of (except by decompiling). This happens by writing a file to the temporary folder that the desktop application will consume, and waiting for that application to "return" by writing another file into temp folder. Folder has to be temp folder.
Question
Is there a Javascript way to access the file system in a non sandboxed way, even by setting specific browser configuration options?
Environment
The application runs on a private LAN where all computers trust each other within the same AD domain. Plus we are theoretically allowed to map any network drive on any computer. Working in the trust zone, we don't have to care about most security concerns.
Background explanation
I have a PowerBuilder desktop application that I need to "port" to web (using J2EE) and install to a separate server, local to the same LAN. This application (named GP) currently starts a process of the child application (GC) that will not show any UI, instead it will listen for two files on a temporary directory.
When GP needs to open a window from the GC process, it will write two files: GP_to_GC.txt with a syntax I have documentation of, and GP_to_GC.flg that serves as flag file. GC will delete both after retaining and parsing the content of txt file, thus showing the appropriate UI form based on input.
The "return" is exactly the opposite. GC will write a pair of GC_to_GP files to temp and GP will refresh its views.
How can I do this with JavaScript?
Based on what I'm reading, you are porting a Desktop based Java EE application to a web application? If so, then you can continue using Java and access the folder that way; no need to use JavaScript.
What is unclear is that you're saying you can't modify anything, yet you're trying to port an application. Which is it?
If you can't modify anything, and the only thing you can do is drop JavaScript into an existing web application, then your solution is rather simple:
Create an HTTP API application that sits somewhere that has access to those folders, and issue a POST request to that API that will then read and write to the file system. It can be in any server side language you choose: Javascript (Node.js), C#, Java, PHP, whatever. It simply needs the ability to access the file system through the webserver, and most frameworks provide that capability.
I can't give you any code because you haven't mentioned which server side language you want to use to accomplish this approach.

Check server version from javascript

In our development environment we use jetty, in our production we use tomcat.
For some functionality we use javascript but there are some hardcoded locations for the use of jetty or tomcat.
I know it's a bit weird to use two server versions but it just the way it is.
So now when we are building the application, sometimes people forget to change the server version in the javascript file.
Is there a way to automatically check if the server is jetty or tomcat from javascript?
I was thinking of placing an txt file in the root of tomcat and let it check whether or not it exists but maybe there is a way to do it more natively.
Assuming one/both of your servers are sending the Server HTTP header (and Jetty usually does, and can easily be configured to do so), then you could use an XMLHttpRequest and look at the response headers.
Read more here: Accessing the web page's HTTP Headers in JavaScript
However, I would suggest that you extract the pieces of code that change between servers into 1 javascript file. e.g:
/* server_info.js */
locations = {
file1 : "/some/path",
file2 : "/another/path"
};
And include that file as a <script> in all your pages.
Then you can have Jetty and Tomcat each use a different version of that file. It should be easy enough to have a servlet (or filter, or action, or whatever exists in your framework) that looks at the server type and serves up the right file.
If that's too much, then you could do the same thing, but simply have:
/* server_info.js */
server_type = "tomcat";
And vary that file by server (you could easily generate that file in a JSP, or something similar)
Obligatory warning: As I'm sure you know, having different servers in dev and prod is not a fantastic idea, for these sorts of reasons. Once you implement a solution to this problem, how are you going to know that the tomcat code works?
Jetty is more than capable of being a production server, and tomcat can do a good job in development. I suspect you (as a team) are making more work for yourselves than really ought to be.
you could make an ajax request to the server and if each one responds in a unique way, you'll know which is which. whether that's the existence of a different file or different content in a particular file, there are many ways the servers can differentiate themselves to the client.

Categories

Resources