iT邦幫忙

0

(Node入門) 書中範例問題

  • 分享至 

  • xImage

大家好,我最近讀了一本覺得不錯的node入門教學(網址如下)
https://www.nodebeginner.org/index-zh-tw.html#handling-post-requests

一路跟著範例寫都很順利,但是最後一個要上傳圖片的範例出現錯誤
https://ithelp.ithome.com.tw/upload/images/20220410/201472322KbJwPLH2n.png
網頁功能:提供一個可以上傳圖片的網頁,上傳後可以再另一個頁面顯示圖片
https://ithelp.ithome.com.tw/upload/images/20220410/201472320MSOYSrEoT.png
程式碼
index.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);

server.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 + "recvived.");
        route(handle, pathname, response, request);
    }
    http.createServer(onRequest).listen(8888);
    console.log("Server has started.");
}

exports.start = start;

router.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 requet handle found for " + pathname);
        response.writeHead(404, {"Content-Type": "text/html"});
        response.write("404 Not found");
        response.end();
    }
}

exports.route = route;

requestHandlers.js

var querystring = require("querystring"),
    fs = require("fs"),
    formidable = require("formidable");

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");
        fs.renameSync(files.upload.path, "/tmp/test.png");
        response.writeHead(200, {"Content-Type": "text/html"});
        response.write("received image:<br/>");
        response.write("<img src='/show' />");
        response.end();
    });
}
  
  function show(response) {
    console.log("Request handler 'show' was called.");
    fs.readFile("/tmp/test.png", "binary", function(error, file) {
        if(error) {
            response.writeHead(500, {"Content-Type": "text/plain"});
            response.write(error + "\n");
            response.end();
        } else {
            response.writeHead(200, {"Content-Type": "image/png"});
            response.write(file, "binary");
            response.end();
        }
    });
  }

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

我電腦是win-10
不知道有沒有人看這個教學也遇到同樣的錯誤呢?
謝謝!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
小魚
iT邦大師 1 級 ‧ 2022-04-10 07:43:26

要學會看錯誤訊息,
看起來應該是你傳入的參數型態錯了,
Server沒有辦法處理.

行數訊息裡面也寫給你了,
到那個地方去找問題就好了.

tsungmin iT邦新手 4 級 ‧ 2022-04-10 21:40:21 檢舉

好的,好像是oldPath那部分錯誤,我再去了解一下fs.renameSync()的用法,感謝回答

2

錯誤表示:
你的 oldPath 參數類型錯誤
你可以將他輸出看看會是什麼
輸出參考:console.log(typeof oldPath)

tsungmin iT邦新手 4 級 ‧ 2022-04-10 21:41:39 檢舉

竟然是undefined...

我要發表回答

立即登入回答