node搭建简单服务器过程:
(1)加载http核心模块
在node中专门提供了一个核心模块:http,这个模块就是帮你创建编写服务器的
var http = require('http')
(2)使用http.createserver()方法创建一个web服务器, 返回一个server实例
var server = http.createServer()
(3)服务器可以用来干嘛:
提供对数据的服务
发请求
接收请求
处理请求
给个反馈(也叫发送响应)
注册request事件
当客户端请求过来,就会自动触发服务器的request请求事件,然后执行第二个参数,回调处理
server.on('request', function (request, response) {
// 1.获取请求路径
var url = request.url
// response.end(url)
// 2.判断路径处理响应
if (url === '/') {
// response.write('index')
// response.end()
response.end('首页')
} else if (url === '/login') {
response.write('登录')
response.end()
} else if (url === '/register') {
response.write('注册')
response.end()
} else if (url === '/hahaha') {
response.write('哈哈哈')
response.end()
} else if (url === '/products') {
var products = [
{
name: '苹果',
price: 8888
}
]
// 响应内容只能是二进制数据或者字符串
response.end(JSON.stringify(products))
}
})
(4)启动服务器:绑定端口号
server.listen(3000, function () {
console.log('服务器启动成功了,可以通过http://127.0.0.1:3000/')
})
完整代码:
var http = require('http')
var server = http.createServer()
server.on('request', function (request, response) {
// 1.获取请求路径
var url = request.url
// response.end(url)
// 2.判断路径处理响应
if (url === '/') {
// response.write('index')
// response.end()
response.end('首页')
} else if (url === '/login') {
response.write('登录')
response.end()
} else if (url === '/register') {
response.write('注册')
response.end()
} else if (url === '/hahaha') {
response.write('哈哈哈')
response.end()
} else if (url === '/products') {
var products = [
{
name: '苹果',
price: 8888
}
]
// 响应内容只能是二进制数据或者字符串
response.end(JSON.stringify(products))
}
})
server.listen(3000, function () {
console.log('服务器启动成功了,可以通过http://127.0.0.1:3000/')
})
读文件操作:
(1)核心模块是fs
var fs = require('fs')
(2)读文件有一个参数,一个回调函数
参数:文件所在位置
回调函数:失败了,怎么样;成功的话,数据都在data中,如果出现乱码,可以使用转换字符串
fs.readFile('./resouce/index.html', function (err, data) {
if (err) {
response.setHeader('Content-Type', 'text/plain;charset=utf-8')
response.end('文件查找错误,请稍候重试')
} else {
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.end(data)
}
完整实例代码:
var http = require('http')
var fs = require('fs')
var server = http.createServer()
server.on('request', function (request, response) {
// 在服务端默认发送的数据,其实是utf8编码的内容
// 但是浏览器不知道你是utf8编码的内容
// 浏览器在不知道服务器响应内容的编码的情况下会按照当前操作系统的的默认编码去解析
// 相当于是你发的是英语,却用汉语去解析
// response.setHeader('Content-Type', 'text/plain;charset=utf-8')
// response.end('hello,世界')
var url = request.url
if (url === '/') {
response.setHeader('Content-Type', 'text/plain;charset=utf-8')
response.end('hello,世界')
} else if (url === '/html') {
// response.setHeader('Content-Type', 'text/html;charset=utf-8')
fs.readFile('./resouce/index.html', function (err, data) {
if (err) {
response.setHeader('Content-Type', 'text/plain;charset=utf-8')
response.end('文件查找错误,请稍候重试')
} else {
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.end(data)
}
})
} else if (url === '/png') {
fs.readFile('./resouce/abc.png', function (err, data) {
if (err) {
response.setHeader('Content-Type', 'text/plain;charset=utf-8')
response.end('文件查找错误,请稍候重试')
} else {
response.setHeader('Content-Type', 'image/png;charset=utf-8')
response.end(data)
}
})
}
})
server.listen(3000, function () {
console.log('服务器启动成功了,可以通过http://127.0.0.1:3000/')
})
data的变化思维导图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OaL5KhVg-1617331003163)(D:\node知识\f16f5d25de659d1b2d31c0d3806c882.png)]
添加第二个参数utf8将把读取到的文件直接按照utf8编码转成我们认识的字符
fs.readFile('./db.json', 'utf8', function (err, data) {
if (err) {
return res.status(500).send('Server error')
}
})
通过require执行多个文件:
require方法有两个作用:
一,加载文件模块并执行里面的代码
require是一个方法,他的作用就是用来加载模块的。在node中,模块有三种:
(1)具名的核心模块,例如fs,http
(2)用户自己编写的文件模块
例子1:
a文件中的代码:
console.log('a文件被执行了')
var foo = 'aaa'
require('./b.js')
console.log('a文件执行结束')
console.log(foo)
b文件中的代码:
console.log('b被执行了')
var foo = 'bbb'
最终输出结果:
这个foo并没有被覆盖,输出的结果仍然是aaa
在node中,没有全局作用域,只有模块作用域,外部访问不到内部,内部也访问不到外部
require(‘b’)这是在引入核心模块
require(‘./b’)这是在引入具名模块
二,拿到被加载文件模块导出的接口对象(针对多个成员)
在每个文件模块中都提供了一个对象:exports,exports默认是一个空对象
做法:
首先将需要导出的变量和方法挂载到exports身上
var foo = 'bbb'
exports.foo = 'hello'
exports.add = function (x, y) {
return x + y
}
require就可以接收这个导出的exports对象,然后再按照对象的方式去使用
console.log('start a')
var bExports = require('./b')
console.log(bExports.foo)
console.log(bExports.add(10, 20))
三,拿到被加载文件模块导出的接口对象(针对单个成员)
module.exports只可以导出一个,像下边的这种写法,最终导出函数,函数会把前边的字符串给覆盖住
在b文件中:
var foo = 'bbb'
module.exports = 'hello'
module.exports = function (x, y) {
return x + y
}
在a文件中:
console.log('start a')
var bExports = require('./b')
console.log(bExports)
也可以通过这种方式导出多个成员:
var foo = 'bbb'
module.exports = {
add: function () {},
foo: 'hello'
}
mongdob的下载安装:
下载网址https://www.mongodb.com/try/download/community?tck=docs_server
首先下载安装包,然后进行安装,安装完成之后,在电脑属性中需要配置一下环境变量,比如复制文件的路径添加到环境变量中
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YDbcLv6I-1617331088281)(D:\node知识\0957cfc6921117eb353f84f350b164f.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v09ywfJX-1617331088283)(D:\node知识\f2dcc0c400cfcacb68417cb3e4ab498.png)]
输入以下代码查看版本信息和安装是否成功:
mongod --version
mongdob的开启和关闭:
1.启动方式:
打开控制台,输入mongod启动,但是前提是得在你安装的那个盘中有这样的文件夹 /data/db
2.关闭方式:
crtl+c
3.如果想要修改默认的数据存储记录,可以
mongod --dbpath=数据存储目录路径
4.连接数据库
在命令行输入mongo,就会连接你本地的数据库
mongo
5.退出数据库
在连接状态输入exit退出连接
exit
基本命令:
(1)查看数据库列表
这是数据库的系统,不要动下边的东西
admin 0.000GB
local 0.000GB
show dbs
(2)查看当前操作的数据库
默认会连接到test
db
test
db
(3)切换到指定的数据库(如果没有会新建)
本来数据库的默认名字是test,通过use + 数据库的名字 就会重新建立一个数据库
use itcast
(4)插入名字为students的数据表,格式如下
1.这个时候你再show dbs,这个itcast数据库就会被显示出来了
db.students.insertOne({name:"jack"})
2.第二种方法,创建集合
db.createCollection("students")
(5)显示当前数据的所有集合
你之前创建的students就会被显示出来
show collections
(6)如何显示这个集合中所有创建的数据
find是显示所有
db.students.find()
{ "_id" : ObjectId("60178139687180c1588e3176"), "name" : "jack" }
(7)删除不需要的数据库
首先连接上数据库
use 数据库名字
删除数据库
db.dropDatabase()
一mongoose的安装:
官网代码:https://mongoosejs.com/
(1)首先创建一个空的文件夹
(2)cd进入新创建的文件夹,执行以下代码:
npm init -y
(3)再次执行以下代码
npm i mongoose
示例代码:
// 引包
const mongoose = require('mongoose');
// 链接mongodb数据库
mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true, useUnifiedTopology: true});
// 创建一个模型,说白了就是在设计数据库
const Cat = mongoose.model('Cat', { name: String });
const kitty = new Cat({ name: 'Zildjian' });
kitty.save().then(() => console.log('meow'));
express的安装过程和简单使用:
一.如何安装
(1)创建一个文件夹
(2)然后快速生成一个文件,通过以下代码
npm init -y
(3)然后正式安装express,通过以下代码
npm i -S express
二.基本使用
在创建的文件夹下创建一个app.js的文件
// 安装
// 1.引包
var express = require('express')
// 2.创建服务器应用程序
// 也就是原来的http.createserver
var app = express()
app.get('/', function (req, res) {
// response.setHeader('Content-Type', 'text/plain;charset=utf-8')
res.end('hello express')
})
app.get('/about', function (req, res) {
res.setHeader('Content-Type', 'text/plain;charset=utf-8')
res.end('世界你好')
})
app.listen(8080, function () {
console.log('app正在运行')
})