httpdispatcher中的setStatic函数

我在node.jsbuild立一个服务器,但是我不能理解httpdispatcher中的setStatic在这个例子中,我在网上的function..

var http = require('http'); var dispatcher = require('httpdispatcher'); //Lets define a port we want to listen to const PORT=8080; //We need a function which handles requests and send response function handleRequest(request, response){ //response.end('It Works(Refereshed)!! Path Hit: ' + request.url); try{ console.log(request.url); dispatcher.dispatch(request,response); }catch(err){console.log(err);} } //set all js/html/css as static during response dispatcher.setStatic('resources'); dispatcher.onGet("/page1",function(req,res){ res.writeHead(200,{'Content-Type':'text/plain'}); res.end("Page one"); }); dispatcher.onPost("/page2",function(req,res){ res.writeHead(200,{'Content-Type':'text/plain'}); res.end("Get Post Data"); }); //Create a server var server = http.createServer(handleRequest); //Lets start our server server.listen(PORT, function(){ //Callback triggered when server is successfully listening. Hurray! console.log("Server listening on: http://localhost:%s", PORT); }); 

但是当我运行这个文件和types的URL像http:// localhost:8080 /资源/ dwnl.png那么我在terminal[TypeError:参数path.join必须是string],并没有显示在浏览器上,但是我有一个资源文件夹下的dwnl.png

这是图像

添加以下行

 dispatcher.setStaticDirname(__dirname); 

之前

 dispatcher.setStatic('resources'); 

并尝试使用http:// localhost:8080 / resources / dwnl.png

请参阅Node.js找不到基本函数这个答案解释说,httpdispatcher必须在使用之前使用类似的东西实例化:

 var httpdispatcher = require('httpdispatcher'); var dispatcher = new httpdispatcher(); 

否则你将得到运行时TypeErrors,因为调度程序不存在。