Node
R 读取
E 执行
P 打印
L 循环
node.js模块:内置模块 自定义模块 第三方模块
启动命令 :node 文件名
path 路径
path.basename() 取路径最后一层
path.dirname() 去掉路径最后一层
path.join() 拼接路径
path.parse() 路径转对象
path.format() 对象转字符串
var path = require('path')
var testPath="d:/ll/node/test/test.js"
console.log(path.basename(testPath)); //test.js
console.log(path.dirname(testPath)); //d:/ll/node/test
var newPage=path.join(__dirname,'test.js') //__dirname 根目录
console.log(newPage); //D:\ll\node\wwwroot\node\test.js
var objPath=path.parse(newPage)
console.log(objPath);
//{
// root: 'D:\\',
// dir: 'D:\\ll\\node\\wwwroot\\node',
// base: 'test.js',
// ext: '.js',
// name: 'test'
//}
var strPath=path.format(objPath)
console.log(strPath); //D:\ll\node\wwwroot\node\test.js
fs 文件
fs.writeFile(路径,内容,回调) 创建文件(如果文件存在,只覆盖内容)
fs.appendFile(路径,内容,回调) 创建文件(如果文件存在,追加内容)
fs.readFile(路径,回调) 读取文件内容
fs.readFile(路径,编码格式 ,回调) 读取文件内容
var fs=require("fs")
fs.writeFile("./test.txt","hello World",function(err){
if(err){
console.log(err);
return
}
console.log("文件创建成功");
})
fs.appendFile("./test.txt","hello World",function(err){
if(err){
console.log(err);
return
}
console.log("内容已拼接");
})
fs.readFile("./test.txt",function(err,data){
if(err){
console.log(err);
return
}
console.log(data); //<Buffer 68 65 6c 6c 6f 20 57 6f 72 6c 64 68 65 6c 6c 6f 20 57 6f 72 6c 64>
console.log(data.toString()); //hello worldhello world
})
fs.readFile("./test.txt","utf8",function(err,data){
if(err){
console.log(err);
return
}
console.log(data); //hello worldhello world
})
http
var http=require("http")
var server = http.createServer()
//监听用户请求
server.on("request",function(req,res){
//req 请求 接受用户请求
//res 响应
// console.log(req.headers); //请求头
// console.log(req.url); //请求地址
res.setHeader('Content-Type','text/html;charset=utf-8')
res.write("111")
res.write("222")
res.end('Hello')
})
server.listen(8080,function(){
console.log("服务已启动 访问地址 localhost:8080");
})
url 地址
url.parse(地址,bool) 地址转对象(bool为true时 query为对象类型 , bool为false query为字符串)
var url=require("url")
var testUrl="http://localhost:8080/doadd?name=ll&sex=男"
// console.log(url.parse(testUrl));
console.log(url.parse(testUrl,true));
加载学生列表(案例)
//student.js
// 创建服务器
var http=require('http')
var fs= require('fs')
var url = require("url")
var server=http.createServer()
var studentList=[
{name:"ll1",sex:"男",age:19,time:'2021-10-6'},
{name:"ll2",sex:"男",age:19,time:'2021-10-6'},
{name:"ll3",sex:"男",age:19,time:'2021-10-6'},
{name:"ll4",sex:"男",age:19,time:'2021-10-6'},
]
server.on('request',function(req,res){
// console.log("正在监听用户请求");
var urls=req.url //获取用户请求的地址
if(urls=="/"){
//说明用户访问的是localhost:8080
fs.readFile('./index.html','utf8',function(err,data){
if(err){
console.log(err);
return
}
var html=""
studentList.forEach((item)=>{
html+=`<tr>
<td>${item.name}</td>
<td>${item.sex}</td>
<td>${item.age}</td>
<td>${item.time}</td>
</tr>`
})
var data= data.replace('@@',html) //通过替换在index.html 里的特殊字符追加数据
res.writeHead(200,{'content-Type':'text/html;charset=utf-8'})
res.end(data)
})
}else if(urls=="/add"){
//说明用户访问的是localhost:8080/add
fs.readFile("./add.html","utf-8",function(err,data){
if(err){
console.log(err);
return
}
res.writeHead(200,{'content-Type':'text/html;charset=utf-8'})
res.end(data)
})
}else if(urls.indexOf('/doadd')==0){
if(req.method=='POST'){
//用户使用post提交
}else{
//用户使用get提交
var params=url.parse(req.url,true).query
console.log(params);
var newStu={
name:params.name,
sex:params.sex,
age:params.age,
time:"2021"
}
studentList.push(newStu)
res.statusCode=302
res.setHeader("Location",'/') //改变路径到localhost:8080
res.end()
}
}
// res.writeHead(200,{'content-Type':'text/html;charset=utf-8'})
// res.end('数据已经返回') //直接返回会出现乱码 需要设置响应头
})
server.listen(8080,function(){
console.log("服务已启动 访问地址 localhost:8080");
})
<!--index.html-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>学生列表页面</title>
</head>
<body>
<table style="width:800px;margin :30px auto ;text-align :center;line-height:40px;" cellpadding="0" cellspacing="0" border="1" >
<tr>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>添加时间</td>
</tr>
@@
</table>
</body>
</html>
<!--add.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>新增</title>
</head>
<body>
<form action="/doadd" method="get">
<table>
<tr>
<td>姓名: <input type="text" name="name"/></td>
<td>性别: <input type="text" name="sex" /></td>
<td>年龄: <input type="text" name="age"/></td>
<td><button type="submit">提交</button></td>
</tr>
</table>
</form>
</body>
</html>
npm
安装模块
npm install 模块名
–save //安装到生产环境(默认就是生产环境)
–save–dev //安装到开发环境 (只能在本地运行)
-g //安装到全局中
卸载模块
npm uninstall 模块名
npm init 初始化项目
安装nrm
npm i nrm -g
nrm ls 查看所有服务器
nrm use taobao 通过use切换服务器
nrm test taobao 测试各个服务器的速度
nodemon
npm install nodemon -g
修改不再需要重新启动服务
启动命令不再是 node test.js
启动命令 nodemon test.js