Nodejs学习(二)---基础常用模块

1.http
2.fs
3.path
4.url
5.querystring

1.http–服务器

nodejs的运用基础是起码要创建一个服务,在该服务的基础上处理业务操作,对于创建一个服务器最原始的莫过于是使用http模块。

//引入http模块
const http = require('http');

//创建服务器
let server = http.createServer((req,res)=>{
	console.log(req.url)
	res.write('aaa');
	res.end(); // 结束下你的请求
})
//监听端口
server.listen(8080);

createServer接受两个参数
request:获取客户端发送的请求数据的信息(请求的地址,请求的方式等)
response:返给客户端的相应数据

2.fs —文件模块

nodejs中尝尝也会操作各种各样的文件,这个时候我们可以用fs模块

const fs = require('fs');

// 属于异步操作 不会耽误其他的执行  
// fs.writeFile(path,data,callback);
// fs.readFile(path,callback);
// 同步形式也有 很少用  太慢了
// fs.writeFileSync();
// fs.readFileSync();

fs.writeFile('./a.txt','xxasxssx',(err)=>{
  if(err){
    console.log('写入失败',err);

  }else{
    console.log('写入成功',err);
  }
});

// data返回的是原始的二进制数据(Buffer) 浏览器是可以解析的 只是你自己看的时候看不懂
fs.readFile('./a.txt',(err,data)=>{
  if(err){
    console.log('读取失败',err);
  }else{
    console.log('读取成功',data.toString());
  }
})

fs.writeFile(path,data,callback); //写入文件 路径,要写入的数据,回调函数 (接受一个err参数 )
写入一旦成功 即使没有这个文件 也会在目录中创建这个文件 并把数据写入到文件中 回调中的 err 返回为 null
写入失败 回调中的err会返回一个错误对象 。

fs.readFile(path,callback); // 用来读文件 不需要data 路径,回调函数(接受err,data参数 data是读取到的数据)
data返回的是原始的二进制数据(Buffer) 浏览器是可以解析的 只是你自己看的时候看不懂。通常我们可以data.toString()转为字符串来查看数据。

3.path — 路径模块

path 模块提供用于处理文件路径和目录路径的实用工具。

const path = require('path');

let str = '/root/a/b/1.txt';

//path.dirname(str) 获取目录名称  /root/a/b
console.log(path.dirname(str)); 

//path.extname(str) 获取扩展名 .txt
console.log(path.extname(str));

//path.basename(str) 获取文件名 1.txt
console.log(path.basename(str));

//path.resolve(str) 对路径的解析 类似于命令行的操纵  \root\a\b\c\d\e
console.log(path.resolve('/root/a/b','c','d','e'));
// 常用的是解析当前的绝对路径
console.log(path.resolve(__dirname,'build'));

__dirname 是node的一个全局变量,获得当前文件所在目录的完整目录名(绝对路径)。

__filename也是node的一个全局变量,变量获取当前模块文件的带有完整绝对路径的文件名(xxx/xx.js)。

4.url

url 模块用于处理与解析 URL。

url.parse(url) 这个方法可以将一个url的字符串解析并返回一个url的对象

const url = require('url');

url.parse("http://user:pass@host.com:8080/p/a/t/h?query=string#hash");
/*
返回值:
{
  protocol: 'http:',
  slashes: true,
  auth: 'user:pass',
  host: 'host.com:8080',
  port: '8080',
  hostname: 'host.com',
  hash: '#hash',
  search: '?query=string',
  query: 'query=string',
  pathname: '/p/a/t/h',
  path: '/p/a/t/h?query=string',
  href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
 }
没有设置第二个参数为true时,query属性为一个字符串类型
*/

url.parse(url,true);

url.parse("http://user:pass@host.com:8080/p/a/t/h?query=string#hash",true);
/*
返回值:
 {
  protocol: 'http:',
  slashes: true,
  auth: 'user:pass',
  host: 'host.com:8080',
  port: '8080',
  hostname: 'host.com',
  hash: '#hash',
  search: '?query=string',
  query: { query: 'string' }, // 第二个参数 为true时 
  pathname: '/p/a/t/h',
  path: '/p/a/t/h?query=string',
  href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'
 }
返回的url对象中,query属性为一个对象
*/

url.format(urlObj) 这个方法是将传入的url对象编程一个url字符串并返回。

url.format({
    protocol:"http:",
    host:"182.163.0:60",
    port:"60"
});
/*
返回值:
'http://182.163.0:60'
*/

url.resolve(from,to) resolve这个方法返回一个格式为"from/to"的字符串

url.resolve("http://whitemu.com","gulu");
/*
返回值:
'http://whitemu.com/gulu'
*/
5.querystring

从字面上的意思就是查询字符串,一般是对http请求所带的数据进行解析。

querystring.parse(str) 将字符串解析为一个对象。
querystring.stringify(strObj) 将字符串对象解析为一个字符串。

const querystring = require('querystring');

console.log(querystring.parse('a=12&b=11&c=11')); //{ a: '12', b: '11', c: '11' }
console.log(querystring.stringify({ a: '12', b: '11', c: '11' })) //a=12&b=11&c=11

querystring.escape(str) escape可使传入的字符串进行编码

querystring.escape("name=慕白");
/*
return:
'name%3D%E6%85%95%E7%99%BD'
*/

querystring.unescape(str) unescape方法可将含有%的字符串进行解码

querystring.unescape('name%3D%E6%85%95%E7%99%BD');
/*
return:
'name=慕白'
*/
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值