gpt4 book ai didi

javascript - 控制台无法输出该目录下的文件?

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:38 32 4
gpt4 key购买 nike

这是我的第一个名为 index5.js

的文件
var server = require("./nonblockingserver");
var router = require("./nonblockingrouter");
var requestHandlers = require("./requestHandlers4");
var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
server.start(router.route, handle);

这是我的第二个文件,名为 nonblockingserver.js

var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(handle, pathname, response);
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;

这是我的第三个文件,名为 nonblockingrouter.js

function route(handle, pathname, response) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname](response);
} else {
console.log("No request handler found for " + pathname);
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not found");
response.end();
}
}
exports.route = route;

这是我的第三个文件,名为 requestHandlers4.js

var exec = require("child_process").exec;
function start(response) {
console.log("Request handler 'start' was called.");
exec("ls -lah", function (error, stdout, stderr) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stdout);
response.end();
});
}
function upload(response) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Upload");
response.end();
}
exports.start = start;
exports.upload = upload;

当我在浏览器中写入 http://localhost:8888/start 时,它应该显示目录中的所有文件,但它不会在浏览器上打印任何内容。

最佳答案

尝试改变这个,

response.writeHead(200, {"Content-Type": "text/plain"});

到,

response.writeHead(200, {"Content-Type": "text/plain; charset=utf-8"});

关于javascript - 控制台无法输出该目录下的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23427871/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com