nodebeginner书籍上的问题
发布于 9 年前 作者 maczyt 3039 次浏览 最后一次编辑是 8 年前 来自 问答

为什么会出现这个错误?新手求教,并没有类似php等后台语言 ![VN$PGCE9~H4})TP7`(JU.png 下面贴上我的代码 server.js

var http = require(“http”); var url = require(“url”);

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("end", function (){
		route(handle, pathname, postData);
	});
	// route(handle, pathname, response);
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");

} // console.log(“Server has started.”); exports.start = start;

router.js

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

index.js

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

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

server.start(router.route, handler);

requestHandlers.js

var exec = require(“child_process”).exec; var querystring = require(“querystring”); var fs = require(“fs”); function start(response, postData) { 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" method="post">'+
'<textarea name="text" rows="20" cols="60"></textarea>'+
'<input type="submit" value="Submit text" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();

} function upload(response, postData) { console.log(“Request handler ‘upload’ was called”); response.writeHead(200, {“Content-Type”: “text/plain”}); response.write("You’ve sent the text: "+querystring.parse(postData).text); response.end(); } function show(response, postData) { console.log(“Request handler ‘show’ was called.”); fs.readFile(“http://image.baidu.com/detail/newindex?col=壁纸&tag=全部&pn=1&pid=9400747126&aid=318841024&from=1&tag3=&app_id=&user_id=775966414&is_album=0&sortlog=0&width=1366&height=768&ic=0”, “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;

谢跪

2 回复

先格式化一下

调试呗~,只看出一张图片

回到顶部