0

私はノードのインとアウトを学ぼうとしています。フレームワークを使用してファイルを提供できることは知っていますが、手動で実行しようとしています。「./public/logo.jpg」に jpeg ファイルがあります。localhost:8080 を介してリクエストを送信すると、画像が表示されず、一般的な画像プレースホルダーを含む空白の画面が表示されます。私は何を間違っていますか?ありがとう!

var http=require('http');
var url=require('url');
var fs=require('fs');

// creates a new httpServer instance
http.createServer(function (req, res) {
// this is the callback, or request handler for the httpServer
log('in server callback')
res.ins=res.write;

var parse=url.parse(req.url,true);
var path0=parse.pathname;
console.log(path0)
// respond to the browser, write some headers so the 
// browser knows what type of content we are sending

var serveFile=function(){
    var path='./public'+path0
    fs.exists(path,function(e){
        if(e){
            log('serving file')
            log(path)
            fs.readFile(path,'binary',function(err,data){
                if(data){
                    res.writeHead(200, {'Content-Type': 'image/jpeg'});
                    res.ins(data)
                    res.end()
                }
            })
        }
        else{
            log('no file to serve')
            log(path)
            servePage()
        }   
    })
}
serveFile()
}).listen(8080); // the server will listen on port 8080
4

1 に答える 1