Read A Server Side File Using JavaScript

后端 未结 5 1265
情书的邮戳
情书的邮戳 2021-02-04 03:11

I have on my web server a JS script that I want to be able to read files. My filesystem is like this:

> Root
index.html
read.js
> files
    file.txt
         


        
5条回答
  •  没有蜡笔的小新
    2021-02-04 03:52

    It's not as simple as it sounds and you are going to have to do some research on server vs client.

    You cannot read server side data through Javascript unless you have a connection to the server. Whatever Javascript code that runs on the client's browser will remain on their browser only, even if both are ran on the same computer. What you need is a way to make the client (in this case, the html+javascript website) communicate with the server.

    There are endless ways to do this but the most simple is through a GET request to a server that is serving that text file.

    Try looking into serving static files with NGINX or maybe something like NodeJS, depending on what meets your needs. From there, create a GET endpoint that your Javascript is going to connect to through an XMLHttpRequest (like @MattW. said).

提交回复
热议问题