gpt4 book ai didi

javascript - 在 Node.js 中通过命令行不会出现实际错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:05:41 24 4
gpt4 key购买 nike

我正在尝试使用 Node.js 创建一个具有 GET 和 POST 功能的 HTTP 服务器,用户将上传图像和文件。但问题是,在尝试运行服务器时,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 - Unexpected token )

我怎样才能知道这个错误出在哪里?也许还有很多其他语法错误。

最佳答案

您缺少 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;

关于javascript - 在 Node.js 中通过命令行不会出现实际错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44425155/

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