使用JS在.html文件中检查一个目录中有哪些文件。

huangapple go评论79阅读模式
英文:

Check what files are in a directory using js in .html file

问题

我正在尝试找出如何在我的网站首页的.html文件中读取位于'../mydirectory/images'目录中的文件数量以及哪些文件。我需要这个信息以便根据该目录中的图片数量自动更改我的网站的某些方面。我对Web开发非常新手,但已经进行了一些研究,似乎require.js是用于此目的的工具,但是在尝试访问文件系统时出现了require未定义的错误。

const fs = require('fs');
const path = '../mydirectory/images';

fs.readdir(path, (err, files) => {
  if (err) {
    console.error(err);
  } else {
    console.log(files);
  }
});
英文:

I am trying to figure out how to read what how many/what files are located within '../mydirectory/images' in a .html file that I have that is the landing page for my website. I need this so I can automatically change some aspects of my website, based on how many images are located within that directory. I am very new to web development but have done some research and it seems like require.js would be the tool to use for this, but I am getting an error saying require isn't defined when trying to access the file system.

const fs = require('fs');
const path = '../mydirectory/images';

fs.readdir(path, (err, files) => {
  if (err) {
    console.error(err);
  } else {
    console.log(files);
  }
});

答案1

得分: 1

fs 是内置于 Node.js 中的模块。它无法在浏览器中运行,也不适用于您的需求。

Require.js 是用于处理浏览器中的 AMD 模块的库。Node.JS 默认使用 CommonJS 模块,而非 AMD 模块(但是 fs 无论如何都不能在浏览器中使用,因为它是一个内置模块)。它也不适用于您的需求。

当您的 HTTP 服务器在确定请求 URL 时关心文件系统的布局时,浏览器只知道 URL。

HTTP 不包含任何用于浏览文件系统的功能。

要使客户端代码获得目录列表,首先服务器需要提供一个提供该列表的 URL。

您的 HTTP 服务器可能具有执行此操作的内置功能(例如 Apache HTTPD 的 Directory Listing feature),但您也可以考虑编写一些服务器端代码,以便以客户端更容易处理的格式(如 JSON)呈现数据。

其次,客户端代码需要获取该列表,可能使用 fetch API 并解析其中包含的 URL。

话虽如此,很可能您的更广泛问题最好从一开始就使用服务器端(而不是客户端)代码来解决。

英文:

fs is a module built into Node.js. It will not run in a browser. It is not suitable for your needs.

Require.js is a library for handling AMD modules in the browser. Node.JS uses, by default, CommonJS modules not AMD modules (but fs can't be used in the browser anyway as it is a built-in module). It is not suitable for your needs.

While your HTTP server may care about the layout of the file system when working out what response to give when a URL is requested, the browser only knows about URLs.

HTTP does not include any features designed for exploring file systems.

For your client-side code to get a directory listing, first the server needs to provide a URL which gives that listing.

Your HTTP server might have a built-in feature that does that (such as Apache HTTPD's Directory Listing feature, but you might also consider writing some server-side code that presents the data in a format (such as JSON) which is easier to process client side.

Second, the client-side code needs to get that listing, perhaps using the fetch API and parse the URLs that are in it.

That said, it is quite likely that your broader problem would be better addressed using server-side (and not client-side) code in the first place.

huangapple
  • 本文由 发表于 2023年7月13日 23:00:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定