我正在创建简单的node.js应用程序,以下是创建的4个文件。 File1: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;
server.start(router.route , handle);
FILE2: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 + "recieved");
response.writeHead(200,{"Content-Type":"text/plain"});
var content = route(handle, pathname)
console.log("Contents Is" + content);
response.write("Content is " +content);
response.end();
}
http.createServer(onRequest).listen(8888)
console.log("server has started");
}
exports.start = start;
文件3:ROUTER.JS
function route(handle, pathname)
{
console.log("About to route a request for" + pathname);
if (typeof handle[pathname] === 'function')
{
return handle[pathname]();
}
else
{
console.log("No Request handler found for "+ pathname);
return "404 Not Found";
}
}
exports.route = route;
文件4:requestHandlers.js
function start()
{
console.log("Request handler 'start' was called ");
return "Hello Start";
}
function upload()
{
console.log("Request handler 'upload' was called");
return "Hello Upload";
}
//This allows us to wire the request handlers into the router, giving our router something to route to
exports.start = start;
exports.upload = upload;
现在,当我在终端上启动index.js并在我的浏览器上请求http://localhost:8888/start时,输出为:
$ node index.js server has started request for/recieved About to route a request for/ Request handler 'start' was called Contents Isundefined
所以在server.js行// var content = route(handle,pathname)没有返回正确的值" Hello Start" 。 我刚刚开始倾斜,任何人都能说出什么问题?