14-读取图片

在这里插入图片描述

var http=require('http')
var fs=require('fs')
var path=require('path')

http.createServer( function (req,res) {

    if(req.url==='/' || req.url==='/index'){

        var file_path=path.join(__dirname,'htmls','index.html')
        fs.readFile(file_path,function (err,data) {
            if(err){
                throw err;
            }
           
            res.end(data)
        })
        
        /*   读取图片也要手动设置*/
    }else  if(req.url==='/images/pcs.png' ){
        fs.readFile(path.join(__dirname,'images','pcs.png'),function (err,data) {
            if(err){
                throw err;
            }
            
            /* 图片文件对应的传输类型 开源中国网站查询 HTTP Mime-type*/
            
            res.setHeader('Content-type','images/png')
            res.end(data)
        })
    } 
}).listen(9090,function () {
    console.log('服务器启动了 请访问: http://localhost:9090')
}

猜你喜欢

转载自blog.csdn.net/MDZZ___/article/details/91377443