初识node.js

初识node.js

模块的引入

const module = require("模块名称")

模块的引入使用require,node.js中有很多模块,其中我们经常用的模块有,fs,url和http模块,

fs模块

  • 文件的异步读取
 fs.readFile("./html/index.html","utf-8",(err,data)=>{
   console.log("err",err)
    console.log("data",data)
 })

fs为引入的fs模块,readFile是fs的异步读取方法,

  • fs.readFileSync是文件同步读取的方法,参数是一个url 的地址,

  • 给文件命名

fs.rename('改前','改后',err=>{})
fs.renameSync("改前","改后")
fs.renameSync("./html/index.html","./html/about.html")
  • fs.unlickSync删除文件。参数为文件路径,删除目录使用rmdir

http模块

const = require("http")
const http = http.createServer((req,res)=>{
	//req 相当于请求报文 
	//res 相当于响应报文
	//后端需要设置响应头 后端设置的权重高
	res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"})
	res.write("hello world")
	res.end() //后端需要结束响应 必须写
	
})

app.listen(8000,()=>{
	console.log("server in running...")
})

url模块

//let obj = url.parse(str) 返回值是个对象 数据如下
protocol: 'http:',	协议
slashes: true,	双斜杠
auth: null,   作者
host: 'localhost:8002',  主机
port: '8002',	端口
hostname: 'localhost',  baidu
hash: '#title',	哈希(锚)	search: '?username=sdfsdf&content=234234',	查询字符串
query: 'username=sdfsdf&content=234234',	数据
pathname: '/aaa',	文件路径
path: '/aaa?username=sdfsdf&content=234234',	文件路径
href: 'http://localhost:8002/aaa?username=sdfsdf&content=234234#title'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值