NodeJs问题求助?
发布于 12 年前 作者 canjian215215 4113 次浏览 最后一次编辑是 8 年前

var handle = {};

handle["/"] = requestHandlers.start; handle["/upload"] = requestHandlers.upload; handle["/start"] = requestHandlers.start;

console.log("===handle==="+handle["/upload"]); 用这个怎么打印出来的是undefined?? 我是新手昨天刚学,希望大家指点下!!

4 回复

这个, 楼主描述详细一些吧, 或者是放别人能用命令本地测试的代码, 太短了理解不了的, 比如说这里的 requestHandlers 是什么要有个来头对吧 贴代码的时候记得开头结尾用三个反引号 ``` 的行标记一下~

问一下,怎么放别人能用本地命令测试的代码?? 下面是我用到的几个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;

问题已解决,谢谢大家的关注!!strong text

module.exports.start = start; module.exports.upload = upload;

回到顶部