node.js学习
第一章:环境搭建
1.node环境搭建和运行
直接下载msi包,安装即可。还没定义环境变量,默认目录C:\Program Files\nodejs node -v 查看版本
node xxx.js 启动
2. npm 包管理
最外层创建一个项目目录名test ,在这个test内部执行npm init
npm init :初始化项目。 packge.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
npm install jquery:安装jquery,多了一个 node_modules目录,package.json 的dependency里面会有
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"jquery": "^3.5.1"
}
}
多了一个dependecies 依赖
npm uninstall jquery:卸载这个包
3.cnpm安装
全局安装,在本目录以外也可以使用这个包
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
给别人的代码只给js 和package.json就可以了,不用给node_modules
安装的目录默认在: C:\Users\沈涛\AppData\Roaming\npm\node_modules\cnpm\bin\cnpm
4.npm常用命令
npm update xxx:更新包
npm install :会将这个目录package.json里面dependency参数依赖的包给装了
第二章 node模块
1.全局模块
任何位置都能访问
process.env
process.argv
新建index.js :console.log(process.env)node index执行:
获取到系统的环境变量参数:
HOMEPATH: '\\Users\\沈涛',
JAVA_HOME: 'C:\\Program Files\\Java\\jdk1.8.0_152',
LOCALAPPDATA: 'C:\\Users\\沈涛\\AppData\\Local',
LOGONSERVER: '\\\\DESKTOP-4GI5B75',
M2_HOME: 'D:\\caolihua\\dev\\apache-maven-3.5.4',
MOZ_PLUGIN_PATH: 'D:\\caolihua\\office\\福昕\\Foxit Reader\\plugins\\',
node_path: 'C:\\Program Files\\nodejs\\node_global\\node_modules',
2.系统模块
require 引入进来,不需要单独下载。安装好node以后就自带了。
path模块:处理文件路径
fs模块:用于文件读写
let fs=require('fs')
fs.readFile('a.txt',(err,data)=>{
if(err){
console.log(err)
}else{
console.log(data.toString())
}
})
3.自定义模块
定义:require 自己封装的模块
export ,module,require
index.js:引入mod.js
const mod1=require('mod')
这种写法是在当前目录的node_modules里面查找mod.js
const mod1=require('./mod')
这种写法是在当前目录查找mod.js
mod.js:
写法1:
exports.a=1;
exports.b=2;
写法2:批量导出
module.exports={
a:1,
b:2
}
写法3:导出函数
module.exports=function(){
}
4.核心http模块
目录结构:
index.js文件
let http=require('http')
let fs=require('fs')
http.createServer((req,res)=>{
console.log(req.url)
var url=req.url
fs.readFile('./'+url,(err,data)=>{ //读取当前目录1.html文件,响应给前端
if(err){
res.writeHead(404)
res.end('404 not found')
}else{
res.end(data) //返回数据,不写的话浏览器一直转圈不结束
}
})
}).listen(8888)
第三章 数据交互
1.GET请求
1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8888/aaaa" method="get">
用户名: <input type ="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
index.js
let http=require('http')
let url=require('url')
http.createServer((req,res)=>{
// http://localhost:8888/aaaa?username=1111&password=2222
console.log(url.parse(req.url,true));
let {pathname,query}=url.parse(req.url,true); //pathname 要和url 解析的对象的变量名一样才能映射过去
console.log(pathname,query)
/**
* Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?username=1111&password=2222',
query: [Object: null prototype] { username: '1111', password: '2222' },
pathname: '/aaaa',
path: '/aaaa?username=1111&password=2222',
href: '/aaaa?username=1111&password=2222'
}
*/
}).listen(8888)
2.POST请求