npmjs.com nodejs的第三方模块库(插件)
npm install 模块名 安装
npm uninstall 模块名 卸载
nodemon 命令行工具,保存后重新执行文件
npm install nodemon -g 全局安装
nrm 切换npm下载地址
nrm ls 显示分流地址
nrm use cnpm或者taobao 切换地址
let 变量
const 常量
//引入文件模块
const fs = require("fs");
//引入PATH系统路径模块
const path = require("path");
//path.join("路径","路径")
let finailPath = path.join("a", "b", "c", "d.css");
// a/b/c/d.css文件
//写入文件 fs.writeFile("路径","写入文件的数据对象",[文件配置,编码],callback)
fs.writeFile(
path.join(__dirname, "a.js"),
JSON.stringify(b),
(err) => {
if (err) throw err;
console.log("Export sucess");
} //输出成功
);
//读取文件 fs.readFile("路径",[文件配置,编码],回调函数)
fs.readFile(path.join(__dirname, "a.js"), "utf-8", (err, txt) => {
if (err) throw err;
console.log(txt);
});
// __dirname获取当前文件的绝对路径(文件夹,不包含文件本身)
//npm init -y 生成package.json 项目描述文件,版本,作者,GITHUB地址,使用模块
//npm install --production 只下载运营使用依赖包
//引入HTTP协议
const http = require("http");
//创建服务器
const app = http.createServer();
//URL模块,用于处理URL地址
const url = require("url");
//服务器请求
app.on("request", (req, res) => {
//HTTP状态码
//200请求成功
//404请求的资源没有找到
//500服务器错误
//400客户端请求语法有误
res.writeHead(200, {
//请求返回HTML文本,utf=8编码,可以使用中文
"content-type": "text/html;charset = utf8",
});
//url.parse(req.url,true) true就会将query属性传回的数据改为对象
let a = url.parse(req.url, true).query;
// console.log(a.对象属性);
//.pathname请求地址
let { query, pathname } = url.parse(req.url, true);
// console.log(query.对象属性);
//-------------------------------------------
//获取请求报文 req.headers
console.log(req.headers["具体项"]);
//-------------------------------------------
//获取请求地址 req.url 有了URL模块后,可用pathname代替
// if (req.url == "/index" || req.url == "/") {
if (pathname == "/index" || pathname == "/") {
res.end("欢迎访问首页");
} else {
res.end("Not Found");
}
//获取请求方式 req.method 请求报文:POST发送数据,GET请求数据
if (req.method == "POST") {
res.end("post");
} else if (req.method == "GET") {
res.end("get");
}
});
//监听3000端口
app.listen(3000);
console.log("服务器启动成功");
路由
const http = require("http");
const app = http.createServer;
const url = require("url");
app.on("request", (req, res) => {
//获取请求方式 req.method 请求报文:POST发送数据,GET请求数据
const method = req.method.toLowerCase();
// let { pathname } = url.parse(req.url);
const pathname = url.parse(req.url).pathname;
res.writeHead(200,{
"content-type":"text/html;charset=utf8"
})
if (method == "get") {
if (pathname == "/" || pathname == "/index") {
res.end("来到首页");
} else {
res.end("没找到网页哦");
}
}
});
app.listen(3000);
console.log("服务器启动成功");
POST请求
//引入HTTP协议
const http = require("http");
//创建服务器
const app = http.createServer();
//URL模块,用于处理URL地址
const url = require("url");
//处理请求参数模块
const querystring = require("querystring")
//服务器请求
app.on("request", (req, res) => {
let postData = "";
//post参数是通过data(请求参数传递的时候触发)和end(参数传递完成触发)事件获取
req.on("data",data=>{
postData += data;
})
req.on("end",()=>{
//把字符串参数转成对象形式
console.log(querystring.parse(postData));
})
res.end("结束")
});
//监听3000端口
app.listen(3000);
console.log("服务器启动成功");
静态资源访问
const http = require("http");
const app = http.createServer();
const url = require("url");
const path = require("path");
const fs = require("fs");
//自动判断请求的文件格式
const mime = require("mime");
app.on("request", (req, res) => {
let pathname = url.parse(req.url).pathname;
pathname = pathname == "/" ? "/index.html" : pathname;
//当前文件所在文件夹下的public文件夹下的pathname
let realpath = path.join(__dirname, "public"+pathname);
//获取网页请求的文件类型
let type = mime.getType(realpath);
//读取文件
fs.readFile(realpath, (err, result) => {
//读取失败
if (err != null) {
res.writeHead(404, {
"content-type": "text/html;charset=utf8",
});
res.end("文件读取失败");
return;
}
//读取成功
res.writeHead(200,{
"content-type":type
})
//输出结果
res.end(result);
});
});
app.listen(3000);
console.log("服务器启动成功");