有了ChatGPT编程是否还需要那么多库

其他教程   发布日期:2025年02月07日   浏览次数:217

这篇文章主要介绍了有了ChatGPT编程是否还需要那么多库的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇有了ChatGPT编程是否还需要那么多库文章都会有所收获,下面我们一起来看看吧。

瀑布流展示图片的新需求

我已经可以生成一些图片了,但是光看图片我能看出什么来?我需要展示出图片和图片的信息。这可能就要专门开发一下了。

我当时想,这个可能有点难,他是个前后端都有的程序。所以我就本能的想到,要不要找找有个库没有,先找个瀑布流展示图片的现成的库,在上面稍微改改可能是比较简单的做法。害怕ChatGPT不行,我还去问了new bing,我想着,newbing的知识更新一点,于是我问:

指定一个path直接启动一个nodejs server,通过localhost访问,以瀑布流形式展示path下所有图片的npm工具

结果他建议我使用node-static的库,于是我就追问了一些怎么用node-static库的方式去写代码,得到的代码可能由于知识过时,反正是不能用的,我就不贴在这里了。

有点沮丧,这稍微用点库你就给我撂挑子,你这果然也不行啊,一用库就出错,你这果然胜任不了复杂开发。new bing不靠谱,我就又去问ChatGPT,一开始我问得有问题,我直接问:

用node-static库怎么实现:指定一个path直接启动一个nodejs server,通过localhost访问,以瀑布流形式展示path下所有图片

结果显示的代码果然是不能用的,我心想对啊,new bing都没有新数据,你这个数据岂不是更糟糕?但是灵机一动之下,我也没退出,我就问

上面的代码不能运行,有没有别的办法?

然后我得到了下面的回答:

可以使用Node.js的http和fs模块来启动一个HTTP服务器,并且使用path模块来获取文件路径,以及使用mime模块来获取文件的MIME类型,然后通过HTML和CSS来创建一个简单的瀑布流布局。

以下是一个示例代码:

  1. const http = require('http');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const mime = require('mime');
  5. const PORT = 3000;
  6. const PATH = './path'; // 设置图片目录的路径
  7. http.createServer((req, res) => {
  8. console.log(`Request for ${req.url}`);
  9. // 如果请求的是根路径,返回图片列表页面
  10. if (req.url === '/') {
  11. res.writeHead(200, { 'Content-Type': 'text/html' });
  12. // 读取图片目录下的所有文件
  13. fs.readdir(PATH, (err, files) => {
  14. if (err) {
  15. console.error(err);
  16. res.end('Error reading directory');
  17. return;
  18. }
  19. // 使用HTML和CSS创建瀑布流布局
  20. res.write(`
  21. <html>
  22. <head>
  23. <title>Image Gallery</title>
  24. <style>
  25. .container {
  26. display: grid;
  27. grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  28. grid-gap: 10px;
  29. }
  30. .item {
  31. position: relative;
  32. }
  33. .item img {
  34. width: 100%;
  35. height: auto;
  36. }
  37. .item .caption {
  38. position: absolute;
  39. bottom: 0;
  40. left: 0;
  41. right: 0;
  42. background-color: rgba(0, 0, 0, 0.5);
  43. color: #fff;
  44. padding: 5px;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <div class="container">
  50. `);
  51. // 遍历所有文件,并在瀑布流布局中展示图片和文件名
  52. files.forEach((file) => {
  53. const filePath = path.join(PATH, file);
  54. const mimeType = mime.getType(filePath);
  55. if (mimeType && mimeType.startsWith('image/')) {
  56. res.write(`
  57. <div class="item">
  58. <img src="${filePath}" alt="${file}">
  59. <div class="caption">${file}</div>
  60. </div>
  61. `);
  62. }
  63. });
  64. res.write(`
  65. </div>
  66. </body>
  67. </html>
  68. `);
  69. res.end();
  70. });
  71. } else {
  72. // 否则,返回请求的文件
  73. const filePath = path.join(PATH, req.url);
  74. const mimeType = mime.getType(filePath);
  75. if (mimeType && mimeType.startsWith('image/')) {
  76. // 如果请求的是图片文件,则返回图片数据
  77. fs.readFile(filePath, (err, data) => {
  78. if (err) {
  79. console.error(err);
  80. res.writeHead(404);
  81. res.end('Not found');
  82. return;
  83. }
  84. res.writeHead(200, { 'Content-Type': mimeType });
  85. res.end(data);
  86. });
  87. } else {
  88. // 否则,返回404错误
  89. res.writeHead(404);
  90. res.end('Not found');
  91. }
  92. }
  93. }).listen(PORT);
  94. console.log(`Server running at http://localhost:${PORT}`);

将上述代码保存为app.js文件,然后在终端中进入该文件所在的目录,并运行以下命令: node app.js 运行该命令后,服务器将会在本地3000端口上启动。您可以在浏览器中输入

http://localhost:3000/来查看图片列表,其中/表示根路径,这将会显示图片列表页面,瀑布流形式展示path下所有图片。如果您想直接访问某张图片,可以通过输入其文件名来访问,例如http://localhost:3000/example.jpg。

请注意,在上述示例中,我们假设所有图片文件的MIME类型都以image/开头,这通常是正确的,但如果您的图片文件类型不同,则可能需要修改相关代码。

咦?可以用原生写法啊,我立刻修改了自己的提问:

我需要用nodejs写一个后端服务,可以指定一个文件夹,按瀑布流显示下面所有的png图片。 要求:

可以通过传参指定任意基于index.js的相对文件夹及子文件夹

要并排显示png图片和png图片文件中的文本信息

页面要用BootStrap布局

我有一个模块:'./util/png_info/index'。 该模块导出的对象上有一个异步函数 readPng 可以读取 PNG 图像文件中的文本信息。不用实现,其签名为:

  1. async function readPng(filePath) {
  2. //....
  3. }

指定index.js所在文件夹为静态文件夹

我得到了可以执行的代码:

  1. const express = require('express');
  2. const path = require('path');
  3. const fs = require('fs');
  4. const { readPng } = require('./util/png_info');
  5. const app = express();
  6. // 设置静态文件夹
  7. app.use(express.static(path.join(__dirname)));
  8. app.get('/images', async (req, res) => {
  9. try {
  10. const folderPath = req.query.folder;
  11. const images = await getImages(folderPath);
  12. const imagesHTML = await Promise.all(
  13. images.map(async (image) => {
  14. const info = await readPng(image.path);
  15. return `
  16. <div class="col-md-3">
  17. <img src="${image.url}" class="img-thumbnail" alt="${image.name}">
  18. <div class="text-muted">${info}</div>
  19. </div>
  20. `;
  21. })
  22. );
  23. const html = `
  24. <html>
  25. <head>
  26. <title>Image Gallery</title>
  27. <link href="https://www.19jp.com">cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.2/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet">
  28. </head>
  29. <body>
  30. <div class="container">
  31. <div class="row">
  32. ${imagesHTML.join('')}
  33. </div>
  34. </div>
  35. </body>
  36. </html>
  37. `;
  38. res.send(html);
  39. } catch (error) {
  40. console.error(error);
  41. res.status(500).send('Internal Server Error');
  42. }
  43. });
  44. async function getImages(folderPath) {
  45. const images = [];
  46. const files = await fs.promises.readdir(folderPath, { withFileTypes: true });
  47. for (const file of files) {
  48. if (file.isDirectory()) {
  49. const subfolderPath = path.join(folderPath, file.name);
  50. const subfolderImages = await getImages(subfolderPath);
  51. images.push(...subfolderImages);
  52. } else if (file.name.endsWith('.png')) {
  53. const url = path.join('/', path.relative(__dirname, folderPath), file.name).replace(//g, '/');
  54. images.push({ name: file.name, path: path.join(folderPath, file.name), url });
  55. }
  56. }
  57. return images;
  58. }
  59. const port = process.env.PORT || 3000;
  60. app.listen(port, () => {
  61. console.log(`Server listening on port ${port}`);
  62. });

以上就是有了ChatGPT编程是否还需要那么多库的详细内容,更多关于有了ChatGPT编程是否还需要那么多库的资料请关注九品源码其它相关文章!