15-读取css文件


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;
            }
          
            res.setHeader('Content-type','images/png')
            res.end(data)
        })
          /*   同读取图片道理一样*/
    }else  if(req.url==='/css/index.css' ){
        fs.readFile(path.join(__dirname,'css','index.css'),function (err,data) {
            if(err){
                throw err;
            }
            /*  css文件对应的传输类型 开源中国网站查询 HTTP Mime-type*/
            res.setHeader('Content-type','text/css')
            res.end(data)
        })
    } 
}).listen(9090,function () {
    console.log('服务器启动了 请访问: http://localhost:9090')
})







猜你喜欢

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