3.常用内置模块

OS

path

1.path.basename(path[, ext])

参数解析如下:
path:
ext: 可选的文件扩展名
返回:
用于获取路径的最后一部分

console.log(path.basename("/node/base/path/xx/js/index.js"));

// 最后输出 index.js	

如果只想获取文件名

const path = require('path');

console.log(path.basename("/node/base/path/xx/js/index.js", '.js'));

// 最后输出 index	

2.path.dirname(path)

该方法返回一个path的目录名

const path = require('path');

const filepath = '/node/base/path/index.js';

console.log(path.dirname(filepath)); // 输出 /node/base/path

3.path.parse(path)

path.parse()方法返回一个对象,对象的属性表示path的元素

const path = require('path');
const filepath = '/node/base/path/index.js';
console.log(path.parse(filepath)); 
// 输出如下:
/*
  { 
    root: '/',
    dir: '/node/base/path',
    base: 'index.js',
    ext: '.js',
    name: 'index' 
  }
*/

file 读写文件操作

读 fs.readFile()

fs.readFile('',function(err,data){
	if(err){
	
	}else{
		
	}
})

image

写 fs.write()

fs.write('文件路径',"内容",funtion(err){
	console.log('ok');
})

image

http

创建serve服务器
image
res.end("内容")方法表示写入并关闭,是res.write和res.end()的缩写
image
响应数据时,res.end()中的内容只能是字符串或者二进制数据
image

url

1.url.parse()

url.parse()可以将一个完整的URL地址,分为很多部分,常用的有:host、port、pathname、path、query。

var http = require("http");

var url = require("url");

var server = http.createServer(function(req,res){

    var pathname = url.parse(req.url).pathname;

    var query = url.parse(req.url).query;

    console.log("patname:"+ pathname);

    console.log(query);

    res.end();

});

server.listen(3000,"127.0.0.1");

image

posted @ 2021-11-28 23:25  禾耳  阅读(44)  评论(0)    收藏  举报