内置API
1.fs
文件的读写
const fs = require('fs');
fs.writeFile(__dirname+'/file/text.txt','小红=20 小明=60 小刚=40 小强=80',function(erro){
if(erro)
console.log(erro);
else{
fs.readFile(__dirname+'/file/text.txt','utf-8',function(err,datastr){
if(err)
return console.log(erro);
console.log(datastr)
const arr = datastr.split(' ');
console.log(arr)
const arr1 = []
arr.forEach(element => {
arr1.push(element.replace('=',":"))
});
console.log(arr1)
const arr2 = arr1.join('\r\n')
console.log(arr2)
});
}
})
2.path
路径操作
const path = require('path')
const pathstr = path.join(__dirname,'/file','/text.txt')
const fs = require('fs')
fs.readFile(pathstr,'utf-8',function(error,data){
if(error){
return console.log(error)
}else
console.log(data)
})
console.log(path.basename(pathstr))
console.log(path.basename(pathstr,'.txt'))
console.log(path.extname(pathstr))
3.http
服务器相关
const http = require('http')
const server = http.createServer()
server.on('request', (req,res) => {
const url = req.url
const method = req.method
let content = '<h1>404 Not Found!!!</h1>'
if (url === '/' || url === '/index.html'){
content = '<h1>主页</h1>'
}else if(url === '/shop.html'){
content = '<font color="red">商店</font>'
}
res.setHeader('Content-Type','text/html; charset=utf-8')
res.end(content)
})
server.listen(80,function(){
console.log('server running at http://127.0.0.1')
})
第三方API
npm init -y:创建pakage.json npm i 包名[@版本号]:导入第三方包 npm i 包名[@版本号] -D:导入第三方包,但只是开发阶段使用 npm i:导入pakage.json中所依赖的所有包 npm uninstall 包名:卸载第三方包 //1.切换镜像 npm config get registry:获取当前下载的镜像源地址 npm config set registry=https://registry.npm.taobao.org/ //2.切换镜像 npm i nrm -g nrm ls:查看可用镜像源 nrm use taobao
|