NodeJS -节点初学者手册- show函数未被调用

j2qf4p5b  于 2023-01-01  发布在  Node.js
关注(0)|答案(2)|浏览(106)

我正在遵循节点初学者书籍中的指南,并设法一直读到最后一节,处理文件上传。这本书重点介绍JavaScript调用方法和使用请求和响应,在必要时只以字符串形式包含很少的HTML。
我所有的console.log语句都显示了正确的文件路径,但我得到的不是图像,而是下面的HTML行:

我认为问题出在requestHandlers.js中的这一行:

response.write("<img src='/show' />");

它应该调用requestHandlers.js底部的show函数。查看本文底部的控制台输出,您可以看到执行从未进入show函数。源代码和控制台输出如下所示:

索引.js

var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");

var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;

server.start(router.route, handle);

服务器.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, request);
  }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}

exports.start = start;

路由器.js

function route(handle, pathname, response, request) {
  console.log("About to route a request for " + pathname);
  if (typeof handle[pathname] === 'function') {
    handle[pathname](response, request);
  } 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;

请求处理程序.js

var querystring = require("querystring"), 
        fs = require("fs"),
        formidable = require("formidable");
var path = require('path');
var file = path.join(__dirname, 'tmp', "test.png");

function start(response) {
  console.log("Request handler 'start' was called.");

  var body = '<html>'+
      '<head>'+
            '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'+
      '</head>'+
          '<body>'+
                '<form action="/upload" enctype="multipart/form-data" method="post">'+
                  '<input type="file" name="upload" multiple="multiple"/>'+
                  '<input type="submit" value="Upload file"/>'+
                '</form>'+
          '</body>'+
    '</html>';

    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(body);
    response.end();
}

function upload(response, request) {
  console.log("Request handler 'upload' was called.");
  var form = new formidable.IncomingForm();
  console.log("about to parse");
  form.parse(request, function(error, fields, files) {
    console.log("parsing done");
    console.log("File = "+file);

    fs.rename(files.upload.path, file,  function (error) {
        if(error) {
            console.log("error");
            fs.unlink(file);
            console.log(file);
            fs.rename(files.upload.path, file);
            console.log(file);
        }
    });
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("received image:<br/>");
    response.write("<img src='/show' />");
    response.end();
  });
}

function show(response) {
    console.log("Request handler 'show' was called.");
    response.writeHead(200, {"Content-Type": "image/png"});
    fs.createReadStream(file).pipe(response);
}

exports.start = start;
exports.upload = upload;
exports.show = show;

控制台输出

C:\Users\pdl\Projects\TestSeparateJS>node index.js
Server has started.
Request for / received.
About to route a request for /
Request handler 'start' was called.
Request for /upload received.
About to route a request for /upload
Request handler 'upload' was called.
about to parse
parsing done
File = C:\Users\pdl\Projects\TestSeparateJS\tmp\test.png
error
C:\Users\pdl\Projects\TestSeparateJS\tmp\test.png
C:\Users\pdl\Projects\TestSeparateJS\tmp\test.png
j9per5c4

j9per5c41#

您发送的是纯文本而不是HTML格式的内容,因此浏览器不会以HTML格式运行它,这就是为什么您看到生成的是<img>文本而不是<img>标记。
upload函数中更改以下内容:

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

改为:

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

rbpvctlc2#

使用node.js的较新版本时,您可能还希望将代码files.upload.path更改为files.upload.filepath

相关问题