var handle = {};
handle[“/”] = requestHandlers.start; handle[“/upload”] = requestHandlers.upload; handle[“/start”] = requestHandlers.start;
console.log("===handle==="+handle[“/upload”]); 用这个怎么打印出来的是undefined?? 我是新手昨天刚学,希望大家指点下!!
问一下,怎么放别人能用本地命令测试的代码?? 下面是我用到的几个js文件: 1.index.js // index.js
var server = require(“…/server/server”);
var router = require(“…/route/route”);
var requestHandlers = (“…/requestHandlers”);
var handle = {};
handle[“/”] = requestHandlers.start; handle[“/upload”] = requestHandlers.upload; handle[“/start”] = requestHandlers.start;
var userObject = {}; // equivalent to new Object()
userObject[“lastLoginTime”] = new Date(); userObject[“/upload”] = requestHandlers.upload;
//alert(userObject[“lastLoginTime”]);
console.log("===handle==="+userObject[“/upload”]); console.log("===handle==="+handle[“/upload”]);
server.start(router.route,handle);
2.server.js
// start.js
var http = require(‘http’); var url = require(“url”);
function start(route, handle){ function onRequest(request,response){ var parthname = url.parse(request.url).pathname; console.log('Request For ‘+ parthname + ' received’);
route(handle, parthname);
response.writeHead(200,{"Content-Type":"text/plain"});
response.write("Hello World !");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server Has Started PORT:8888.");
}
exports.start = start;
3.route.js // route.js
function route(handle, pathname){ console.log('About To Route a Rrequest for '+pathname); console.log("-------test----------"+ handle[pathname]); if(typeof handle[pathname] === ‘function’){ handlepathname; } else { console.log("No rquest handler found for "+ pathname); } }
exports.route = route; 4.// requestHandlers.js
function start(){ console.log(“Request handler – start – was called”); }
function upload(){ console.log(“Request handler – upload – was called”); }
exports.start = start; exports.upload = upload;