gpt4 book ai didi

javascript - httpdispatcher 中的 setStatic 函数

转载 作者:行者123 更新时间:2023-11-30 16:38:13 25 4
gpt4 key购买 nike

我正在用 node.js 构建一个服务器,但是我无法理解我在网上得到的这个例子中 httpdispatcher 中 setStatic 的功能。

            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);
});

但是当我运行这个文件并输入类似http://localhost:8080/resources/dwnl.png的url时然后我在终端中得到以下信息 [TypeError: Arguments to path.join must be strings] 浏览器上没有任何显示,但我在资源文件夹下有一个 dwnl.png

this is the image

最佳答案

添加下一行

dispatcher.setStaticDirname(__dirname);

之前

dispatcher.setStatic('resources');

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

关于javascript - httpdispatcher 中的 setStatic 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32392636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com