2017-06-08 45 views
1

我想用GET和POST創建HTTP服務器,使用Node.js,用戶將上傳圖像和檔案。但問題是,在嘗試運行服務器時,Node.js的命令行不會向我顯示實際的錯誤,並指出不存在的某行。不會在Node.js中通過命令行出現實際錯誤

Server.js

var url = require("url"); 
var http = require("http"); 
var formidable = require("formidable"); 


function start(route ,handle) { 
function onRequest(request, response) { 
    var postData = ""; 
    var pathname = url.parse(request.url).pathname; 
    console.log("Request for " + pathname + " received.");  

    request.setEncoding("utf8"); 

    request.addListener("data", function(postDataChunk) { 
     postData += postDataChunk; 
     console.log("Received POST data chunk ' " + 
      postDataChunk + " ' ."); 
    }); 

request.addListener("data", function(chunk) { 
    //called when a new chunk of data was received 
}); 

request.addListener("end", function() { 
    //called when all chunks of data been received 
}); 

    request.addListener("end", function() { 
    route(handle, pathname, response, postData); 
    }); 
} 

    var formidable = require('formidable'), 
    http = require('http'), 
    sys = require('sys'); 

    http.createServer(function(req,res) { 
     if(req.url == '/upload' && req.method.toLowerCase() == 'POST') { 
      //parse a file upload 
      var form = new formidable.IncomingForm(); 
      form.parse(req,function(err,fields,files) { 
       res.writeHead(200, {'content-type' : 'text/plain'}); 
       res.write('received upload \n\n'); 
       res.end(sys.inspect({fields : fields, files : files})); 
      }); 
      return; 
     } 


     //show a file upload form 
     res.writeHead(200, {'content-type' : 'text/html'}); 
     res.end(
     '<form action = "/upload" enctype="multipart/form-data" ' + 
     'method ="post">' + 
     '<input type="text" name= "title" ><br>' + 
     '<input type ="file" name = "upload" multiple="multiple"<br>' + 
     '<input typw="submit" value="upload"' + 
     '</form>'); 
}).listen(8888); 


exports.start = start; 

唯一可以肯定的是,我知道這是在Server.js發生,因爲被指出了那裏。

server.js:69 - 意外的令牌)

我怎樣才能知道這個錯誤是什麼?也許可以是很多其他的sintax錯誤。

回答

0

您錯過了start函數的關閉}。嘗試添加,看看錯誤是否消失。這就是你的代碼應該看起來如何(使用正確的縮進以便更容易地發現錯誤):

var url = require("url"); 
var http = require("http"); 
var formidable = require("formidable"); 

function start(route ,handle) { 
    function onRequest(request, response) { 
     var postData = ""; 
     var pathname = url.parse(request.url).pathname; 
     console.log("Request for " + pathname + " received.");  

     request.setEncoding("utf8"); 

     request.addListener("data", function(postDataChunk) { 
      postData += postDataChunk; 
      console.log("Received POST data chunk ' " + 
       postDataChunk + " ' ."); 
     }); 

     request.addListener("data", function(chunk) { 
      //called when a new chunk of data was received 
     }); 

     request.addListener("end", function() { 
      //called when all chunks of data been received 
     }); 

     request.addListener("end", function() { 
     route(handle, pathname, response, postData); 
     }); 
    } 

    var formidable = require('formidable'), 
    http = require('http'), 
    sys = require('sys'); 

    http.createServer(function(req,res) { 
     if(req.url == '/upload' && req.method.toLowerCase() == 'POST') { 
      //parse a file upload 
      var form = new formidable.IncomingForm(); 
      form.parse(req,function(err,fields,files) { 
       res.writeHead(200, {'content-type' : 'text/plain'}); 
       res.write('received upload \n\n'); 
       res.end(sys.inspect({fields : fields, files : files})); 
      }); 
      return; 
     } 

     //show a file upload form 
     res.writeHead(200, {'content-type' : 'text/html'}); 
     res.end(
     '<form action = "/upload" enctype="multipart/form-data" ' + 
     'method ="post">' + 
     '<input type="text" name= "title" ><br>' + 
     '<input type ="file" name = "upload" multiple="multiple"<br>' + 
     '<input typw="submit" value="upload"' + 
     '</form>'); 
    }).listen(8888); 
} 

exports.start = start; 
+0

不要工作太多,當我把他指出給他,並說他不期望。我不知道這裏發生了什麼,我嘗試了一切,更改代碼,評論代碼的某些部分,但它不會改變任何內容,只是錯誤所在的行。 – Monteiro

+0

@Monteiro你把支架放在哪裏?在粘貼的示例代碼中絕對缺少括號,並且語法沒有任何問題,所以如果在添加括號後發現抱怨,那麼您將它放在錯誤的位置。 – Clonkex

+0

@Monteiro等等,爲什麼你需要'http'和'formidable'兩次? – Clonkex

相關問題