Day21

1-nodejs 中有哪些常用的内置模块

在使用内模块的时候需要先将所需的内置模块进行引入、

OS模块

在nodejs中OS模块提供了与操作系统相关的属性和方法

复制代码
// 导入OS内置模块,必须先进行导入,否则无法使用
const os = require("os")
// 根据操作系统生成对应的换行符
console.log(os.EOL);
// 查看电脑CPU信息
console.log(os.cpus());
// 查看电脑的内容总大小,单位是字节
console.log(os.totalmem());
// 查看点奥的剩余内存大小 单位是字节
console.log(os.freemem());
// 查看电脑主机名称
console.log(os.hostname());
// 查看电脑系统类型
console.log(os.type());

path模块

nodejs总的path模块用于处理文件和目录的路径

复制代码
// 导入内置模块——path
const path = require('path')
// 获取文件名称
console.log(path.basename('c/d/e/filename.js'));
// 获取文件路径
console.log(path.dirname('c/d/e/filename.js'));
// 获取路径中文件夹的后缀名称
console.log(path.extname('c/d/e/filename.js'));
// 将输出的路径进行拼接
console.log(path.join('/c','/d','/e'));
// 模拟cd操作并进行拼接
console.log(path.resolve('c','d','e'));//如果前面没有“/”会自动将当前的绝对路径添加到前面
console.log(path.resolve('/c','/d','e'));//由于c和d前面都具有“/”,表示c和d都是根目录下面的子文件夹,因此d会将c进行替换
复制代码

url模块

在nodejs中url模块是用来解析url地址

url.parse(要解析的内容,是否查询字符串)

是否查询字符串,默认是false,不进行单独解析;可以选填true,进行单独解析。返回值是一个解析好的对象

// 导入url模块
const url = require('url')
// 第二个参数使用默认值
console.log(url.parse("https://mengyuanclub.cn:8080?name=mengyuan&password=123465"));

// 导入url模块
const url = require('url')
// 第二个参数使用默认值
console.log(url.parse("https://mengyuanclub.cn:8080?name=mengyuan&password=123465",true));

 

 在url模块中resolve是以一种Web浏览器解析超链接的方式把一个目标URL解析成一个相对于一个基础的url

console.log(url.resolve('https://lynnn.cn/foo/bar','aa'))
console.log(url.resolve('https://lynnn.cn/foo/bar','../aa'))
console.log(url.resolve('https://lynnn.cn/foo/bar/','aa'))
console.log(url.resolve('https://lynnn.cn/foo/bar/','../aa'))

如果第一个参数后面没有/,第二个参数前面没有/;

如果第一个参数后面没有/,第二个参数前面是./;

如果第一个参数后面有/,第二个参数前面是../;

表示第一个参数最后一个文件夹和第二个参数是同级目录,第二个参数会将第一个参数的最后一个文件夹进行替换

如果第一个参数后面具有/,第二个参数前面没有/;

表示第二个参数直接拼接到第一个参数后面

如果第一个参数后面没有/,第二个参数前面是../;

表示第二个参数和第一个参数的倒数第二个文件夹是同级会直接进行替换

2-常用的第三方模块

第三方模块,别人写好的,具有一定功能,有多个文件组成,放在一个文件中
// 存在形式:
    // 1-以js文件形式存在
    // 2-以命令行工具形式存在,辅助项目开发

// npm
// npm install 模块名称  可以简写为  npm  i 模块名称
// 卸载:npm uninstall  模块名称


// 全局安装和本地安装
    //  -g
    // --save-dev     -D
 
第三方模块 gulp  基于node平台开发的前端打包构建的工具
// html css js压缩合并
// es6 es7语法转换
// 公共文件抽离
// gulp打包基于任务---task
// vue webpack   基于项目

// gulp.src()  获取任务要处理的文件
// gulp.dest()   输出文件
// gulp.task()   创建gulp任务
// gulp.watch()  监听文件变化
// pipe()         流
 
自定义文件
// 系统模块
// 第三方模块   路径+文件名./不能省略
// 以上三种都是用require加载

// 模块的加载机制
// 1-模块有路径但是没有后缀
//        require引入如果是完整得到的路径,直接引入
//        如果模块省略了后缀,查找同名js文件,引入
//         如果找到同名的文件夹,在文件夹中找index.js
//         如果index.js没有,在当前文件夹找package,json查找入口文件
//         如果再找不到入口文件,报错


// 2-模块即没有路径也没有后缀
//    nodejs先认为系统模块
//    如果没有系统模块,去node_modules查找
//    先去查找是否有该名字的js文件
//    再看是否有同名文件夹
//     查看是否有index.js
//     package.json 查看入口文件
//     报错
// var aa = require('./add')
// add.add()

 

3-如何创建一个服务器

// 加载模块
const http = require('http')
// url模块
const url = require('url')
// 创建服务器
var app = http.createServer()
// 监听请求
app.on('request',(req,res)=>{
  // res.write('oook')
  // res.end('你好')
  res.writeHead(200,{
    'content-type':'text/html;charset=utf8'
  })
  // res.end('你好')
  // 区分get和post
  /* if(req.method=='POST'){
    res.end('post')
  } else if(req.method=='GET'){
    res.end('get')
  } */
  // 根据不同请求,返回不同响应
  // req.url
  // console.log(req.url)
  /* if(req.url=='/index' || req.url=='/'){
    res.end('nihao')
  } else if(req.url=='/category'){
    res.end('hello')
  } else {
    res.end('页面找不到')
  } */

4-http 搭建的服务相应给客户端的数据如果是中文,应该如何处理

 

5-第三方 router 模块和 serve-static 如何使用,作用是什么?

  1. 建立项目文件夹并生成项目描述文件
    使用命令npm init -y生成package.json文件

  2. 创建网站服务器实现客户端和服务器端通信
    创建一个app.js文件,然后在里面写创建服务器的内容

/* app.js文件内容 */
// 引入http模块
const http = require('http');
// 创建网站服务器
const app = http.createServer();
// 当客户端访问服务器端的时候
app.on('request', (req, res) => {
	res.end('ok');
});
// 端口监听
app.listen(80);
console.log('服务器启动成功');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  1. 连接数据库并根据需求设计学员信息表
    使用命令npm install mongoose安装MongoDB模块
/* 创建model文件夹和创建connect.js文件并写内容 */
const mongoose = require('mongoose');
// 连接数据库
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true })
	.then(() => console.log('数据库连接成功'))
	.catch(() => console.log('数据库连接失败'))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在原来的app.js文件中导入这个模块

/* app.js文件追加内容 */
// 数据库连接
require('./model/connect');
  • 1
  • 2
  • 3

设计集合规则与创建集合

/* user.js文件内容,这个文件与connect文件同文件夹 */
const mongoose = require('mongoose');
// 创建学生集合规则
const studentsSchema = new mongoose.Schema({
	name: {
		type: String,
		required: true,
		minlength: 2,
		maxlength: 10
	},
	age: {
		type: Number,
		min: 10,
		max: 25
	},
	sex: {
		type: String
	},
	email: String,
	hobbies: [ String ],
	collage: String,
	enterDate: {
		type: Date,
		default: Date.now
	}
});
// 创建学生信息集合
const Student = mongoose.model('Student', studentsSchema);
// 将学生信息集合进行导出
module.exports = Student;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

在下面的路由文件index.js中导入这个模块,模块返回值是一个构造函数

/* index.js文件追加内容 */
const Student = require('../model/user');
  • 1
  • 2
  1. 创建路由并实现页面模板呈递
    使用第三方模块router实现路由,命令npm install router
    使用命令npm install art-template下载模板引擎
    新建一个router文件夹,创建一个index.js文件来专门写路由的内容,在里面导入这个模块,模块返回值是一个函数,用来获取路由对象的
  1. 建立项目文件夹并生成项目描述文件
    使用命令npm init -y生成package.json文件

  2. 创建网站服务器实现客户端和服务器端通信
    创建一个app.js文件,然后在里面写创建服务器的内容

/* app.js文件内容 */
// 引入http模块
const http = require('http');
// 创建网站服务器
const app = http.createServer();
// 当客户端访问服务器端的时候
app.on('request', (req, res) => {
	res.end('ok');
});
// 端口监听
app.listen(80);
console.log('服务器启动成功');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  1. 连接数据库并根据需求设计学员信息表
    使用命令npm install mongoose安装MongoDB模块
/* 创建model文件夹和创建connect.js文件并写内容 */
const mongoose = require('mongoose');
// 连接数据库
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true })
	.then(() => console.log('数据库连接成功'))
	.catch(() => console.log('数据库连接失败'))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在原来的app.js文件中导入这个模块

/* app.js文件追加内容 */
// 数据库连接
require('./model/connect');
  • 1
  • 2
  • 3

设计集合规则与创建集合

/* user.js文件内容,这个文件与connect文件同文件夹 */
const mongoose = require('mongoose');
// 创建学生集合规则
const studentsSchema = new mongoose.Schema({
	name: {
		type: String,
		required: true,
		minlength: 2,
		maxlength: 10
	},
	age: {
		type: Number,
		min: 10,
		max: 25
	},
	sex: {
		type: String
	},
	email: String,
	hobbies: [ String ],
	collage: String,
	enterDate: {
		type: Date,
		default: Date.now
	}
});
// 创建学生信息集合
const Student = mongoose.model('Student', studentsSchema);
// 将学生信息集合进行导出
module.exports = Student;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

在下面的路由文件index.js中导入这个模块,模块返回值是一个构造函数

/* index.js文件追加内容 */
const Student = require('../model/user');
  • 1
  • 2
  1. 创建路由并实现页面模板呈递
    使用第三方模块router实现路由,命令npm install router
    使用命令npm install art-template下载模板引擎
    新建一个router文件夹,创建一个index.js文件来专门写路由的内容,在里面导入这个模块,模块返回值是一个函数,用来获取路由对象的
posted @ 2021-02-20 10:36  景沁同学  阅读(45)  评论(0)    收藏  举报