在我的节点测试工作中,我一直在调用这些函数,每当我调用函数时我都没有在浏览器中得到结果,万一我给出错误的“路由”然后我正确地在浏览器中重新输入错误..
为什么返回结果的函数没有出现在浏览器中?
这是我的功能:
function route (handle, pathname) {
console.log ("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function'){
handle[pathname](); // but nothing display in browser.
} else {
console.log ("No request Handler for " + pathname);
return "404 Not Found"; //it works fine. ( display the message )
}
}
exports.route = route;
我的方法也被正确调用..这是我的方法:
function start() {
return "Hello Start"; // nothing display in browser
}
function upload() {
return "Hello Upload"; // nothing display in browser
}
exports.start = start;
exports.upload = upload;
应该是什么问题?我应该假设在浏览器中得到结果吗?
更新:
在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 + " received.");
response.writeHead(200, {"Content-Type" : "text/plain"});
var content = route(handle, pathname);
response.write(content); //new change;
response.end(); // new change;
}
http.createServer(onRequest).listen(8888);
console.log ("Server has started");
}
exports.start = start;
现在我收到的错误是这样的:
[ERROR] 14:02:14 TypeError
TypeError: first argument must be a string or Buffer
at ServerResponse.OutgoingMessage.write (http.js:743:11)
at Server.onRequest (/Users/mohamedarif/Sites/node/server.js:16:12)
at Server.EventEmitter.emit (events.js:99:17)
at HTTPParser.parser.onIncoming (http.js:1928:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)
at Socket.socket.ondata (http.js:1825:22)
at TCP.onread (net.js:404:27)
这里有什么问题?
答案 0 :(得分:0)
在路由器部分,我更新了我的功能:现在它正常工作。
function route (handle, pathname) {
console.log ("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function'){
return handle[pathname](); //added return;
} else {
console.log ("No request Handler for " + pathname);
return "404 Not Found";
}
}
exports.route = route;