当前位置: 移动技术网 > IT编程>开发语言>JavaScript > nodejs中的path、url、http、fs小综合

nodejs中的path、url、http、fs小综合

2020年07月23日  | 移动技术网IT编程  | 我要评论

Nodejs

1 文件流stream

    var fs=require("fs");
    var writeStream=fs.createWriteStream("要写入的文件地址");//创建一个写流的过程
    //文件如果存在,则会向文件中添加内容。文件不存在,则会增加改文件,并添加内容
    writeStream.write("要写入的内容","字符编码(utf-8)");//写流
    writeStream.end();//标记写流结尾
    writeStream.on("finish",function(){});//标记写流完成
    writeStream.on('error', function() {});//标记写流过程错误

    var readStream=fs.createReadStream("要读取的文件地址",function(){});//创建读流
    var text="";
    readStream.on('data', function(chunk) {// 监听读流的过程
        text += chunk
    })
    readStream.on("end",function(){});//监听读流结尾
    readStream.on("error",function(err_message){//监听读流过程中出现的错误
            console.log(err_messsage);
    });

2 路由

   var url=require("url");
var path="http://www.baidu.com/#index?md=mobile";//url地址中#后面的值叫哈希值 hash
var path2="https://www.baidu.com/?md=mobile";
//url地址?后面的值叫search值 一般是key=value&key=value的形式.这种形式的参数叫query参数

//parse将url地址解析成为一个对象, 对象中会包含路由对象的所有信息
var res=url.parse(path);
//console.log(res);
// Url {
//     protocol: 'http:', 超文本传输协议
//     slashes: true,
//     auth: null,
//     host: 'www.baidu.com', //链接中的域名
//     port: null,
//     hostname: 'www.baidu.com',//链接中的域名
//     hash: '#index?md=mobile', //#后面的所以内容都是hash值
//     search: null,
//     query: null,
//     pathname: '/',
//     path: '/',
//     href: 'http://www.baidu.com/#index?md=mobile'
//   }
var res2=url.parse(path2);
console.log(res2);
// Url {
//     protocol: 'https:',
//     slashes: true,
//     auth: null,
//     host: 'www.baidu.com',
//     port: null,
//     hostname: 'www.baidu.com',
//     hash: null,
//     search: '?md=mobile', //?后面的称search值
//     query: 'md=mobile', 
//     pathname: '/',//地址名称
//     path: '/?md=mobile',
//     href: 'https://www.baidu.com/?md=mobile'
//   }

3 path

node.js path 模块提供了一些用于处理文件路径的小工具
path.extname§返回路径中文件的后缀名,即路径中最后一个’.'之后的部分

4

5 http、fs、url综合运用

var http=require("http");
var fs=require("fs");
var url=require("url");
var path=require("path");
http.createServer(function(request,responde){
    //获取地址中的pathname 
    //url.parse(request.url) 作用是将获取到的url转变成路由对象
    //url.parse(request.url).pathname
    var pathname = url.parse(req.url).pathname;
    //获取后缀名,判断文件类型 path.extname()
     var extname = path.extname(pathname);
    pathname=pathname=="./" ? "要渲染的文件首页地址":pathname;
   
    function getExtType (ext) {
            var str = ''
            switch (ext) {
                case '.html':
                    str = "text/html"
                break;
                case '.css':
                    str = "text/css"
                break;
                case '.js':
                    str = "text/javascript"
                break;
            }
            return str
        }
        var type = getExtType(extname);
        fs.readFile('./page' + pathname, function(err, data) {
        //page文件夹里是html,css,js等文件夹
            if(err) {
                fs.readFile('要渲染的错误文件的文件地址', function(err, errPage) {
                    res.writeHead(404, {"Content-type": `${type};charset=utf-8`})
                    res.write(errPage)
                    res.end()
                })
            } else {
                res.writeHead(200, {"Content-type": `${type};charset=utf-8`})
                res.write(data)
                res.end()
            }
        })


}).listen(8081,function(){
    console.log("服务正在运行");
})

本文地址:https://blog.csdn.net/W8034/article/details/107496360

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网