【发布时间】:2021-02-13 21:20:19
【问题描述】:
我需要在我的服务器中显示一个 html,其中包含一些文本和内容,这些在这里并不重要,并且 第一张图片在给定的文件夹中找到 作为参数。
我找到了另一个答案,其中 fs.readFile 用于直接检索 html 文件,其中图像已经存在,但在这种情况下 我需要为每个图像构建一个 html。。 p>
我试过了,但它没有显示任何图像:
var http = require('http');
const fs = require('fs');
function read_dir(req, res, path="./images") {
let image_urls = fs.readdirSync(path);
let src = path.split("").slice(1).join("");
let image = `<img id="image" src="http://localhost:8888${src}/${image_urls[0]}">`;
res.end(image);
}
function onrequest(request, response) {
// Programs what is going to be sent back to the browser.
console.log("HTTP request received");
// Parses command line arguments
var myArgs = process.argv.slice(2);
response.writeHead(200,
{"Content-Type": 'text/html'}
);
response.write(`<h1>SLIDESHOW AREA</h1>`);
read_dir(request, response, myArgs[0]);
}
// The http.createServer() method turns my computer into an HTTP server.
var server = http.createServer(onrequest);
// The server.listen() method creates a listener on the specified port or path.
// server.listen(port, hostname, backlog, callback);
server.listen(8888);
console.log("Listening on http://localhost:8888/");
【问题讨论】: