I have a simple Javascript application that formats and displays text from a CSV file located on the same server as the HTML and Javascript website.
I want to read the text from CSV file without hard coding the IP address of the server
Right now I am using something like this
window.onload = () => {
addr_of_csv = "xxx.xxx.xxx.xxx/csvfile.csv"
fetchText(addr_of_csv).then(result => {
console.log(result)
});
}
What is the best practice to load the text file without using the IP address stored in a string?
I tried to use something like var ip = request.getRemoteHost();
but it did not find the request library.
How would I implement it? please provide full code including the import
line.
Or is there a way to enter a relative path to the current directory of the Javascript file?
Thanks,
CodePudding user response:
If you want to read a CSV file located on the same server as your HTML and JavaScript website, you can use a relative path to the file instead of specifying the server's IP address. For example, if your CSV file is located in a directory called data at the root of your website, you could use the following relative path to access the file:
fetchText('/data/csvfile.csv').then(result => {
console.log(result);
});
This relative path will resolve to the correct location of the CSV file on your server, without requiring you to hard-code the server's IP address.
Note that this approach will only work if your website and the CSV file are located on the same server. If your website and the CSV file are hosted on different servers, you will need to use the server's IP address or domain name to access the file.
Here is an example of how you could use the fetch API to read the CSV file using the relative path:
fetch('/data/csvfile.csv')
.then(response => response.text())
.then(result => {
console.log(result);
});
This code uses the fetch API to send a request to the server for the CSV file, and then uses the response.text()
method to convert the response to a string of text. You can then use the result variable to access the text from the CSV file.
Note that you will need to include the fetch polyfill in your application if you want to support older browsers that do not have native support for the fetch API. You can add the polyfill using a <script>
tag in your HTML, or you can install it using a package manager like npm and import it into your JavaScript code.
For example, you could use npm to install the fetch-polyfill
package
CodePudding user response:
i am not sure, if you would like to use relative path or not, but this function can work with both
function readTextFile(file) {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
alert(allText);
}
}
}
rawFile.send(null);
}
and then call it as
readTextFile("file:///C:/your/path/to/file.txt");
readTextFile("http://test/file.txt");
CodePudding user response:
You can't get the IP address of a server directly using JavaScript, just the host name.
There are two solutions to your quest:
1. Use external DNS lookup
Do a REST call to a DNS service that has the proper CORS settings, such as https://dohjs.org/. This implies that your host is defined in a public name service, e.g. won't work for hosts inside a firewall.
2. Create a REST endpoint that returns the IP address
On your server add a new endpoint that returns the IP address. E.g. call this before you call your get CSV endpoint.