Node.js学习第四天-cnblog

Node.js学习第四天

1. 基本使用

  • 安装
npm i express@4.17.1
  • 创建最基本的web服务器
const express=require('express')
const app=express()
app.listen(80,()=>{
console.log('express server running at http:127.0.0.1')
})
  • 监听请求
app.get(url,function(req,res){
})
app.post(url,function(req,res){
})
  • 响应内容
app.get('/user',(req,res)=>{
// 响应JSON数据
res.send({name:'zs'})
})
app.post('/user',(req,res)=>{
// 响应文本内容
res.send('请求成功')
})
  • 获取req的查询参数
app.get('/',(req,res)=>{
// ?name=zs&age=18
console.log(req.query.name) // zs
})
  • 获取路径参数
app.get('/user/:id',(req,res)=>{
console.log(req.params)
})
  • 托管某个文件夹下的静态资源
app.use(express.static('public'))
app.use(express.static('files'))
  • 托管静态资源加上请求前缀
app.use('/public',express.static('public'))
// http:localhost:3000/public/images/kitten.jpg

2. nodemon

  • 热部署服务器工具
npm i nodemon

3. express 路由

  • router.js
const express=require('express')
const router=express.Router()
router.get('/user/list',(req,res)=>{
})
module.exports=router
  • 注册路由(index.js)
const userRouter=require('./router.js')
app.use(userRouter)
  • 添加路由前缀
app.use('/api',userRouter)

4. Express 中间件

const mw=function(req,res,next){
console.log('这是一个最简单的中间件函数')
// 交给下一个中间件或者路由,必须的
next()
}
// 全局中间件
app.use(mw)

  • 定义多个全局中间件
app.use(function(req,res,next){
console.log('第一个中间件')
next()
})
app.use(function(req,res,next){
console.log('第2个中间件')
next()
})
app.use(function(req,res,next){
console.log('第3个中间件')
next()
})
app.get('/user',(req,res)=>{
// 依次经过上面的中间件才到达这里
res.send('home page')
})
  • 局部中间件
const mv1=function(req,res,next){
console.log('这是中间件函数')
next()
}
// 当前路由生效
app.get('/',mv1,function(){
})
// 不影响该路由
app.get('/user',function(req,res){
...
})
  • 定义多个局部中间件
app.get('/',mv1,mv2,(req,res)=>{
})
app.get('/',[mv1,mv2],(req,res)=>{
})

5. 中间件分类

5.1 应用级别的中间件

  • 绑定在app实例上的中间件
app.use((req,res,next){
next()
})
app.get('/',mv1,function(req,res){
})

5.2 路由级别中间件

const app=express()
const router=express.Router()
// 路由级别中间件
router.use(function(req,res,next){
next()
})
app.use('/user',router)

5.3 错误级别的中间件

  • 错误级别中间件必须注册在所有的路由之后
  • 四个参数(err,req,res,next)
app.get('/',function(req,res){
throw new Error('xxx')
res.send('home page')
})
app.use(function(err,req,res,next){
console.log('发生了错误'+err.message)
})

5.4 Express内置中间件

5.5 第三方中间件

6. 跨域资源共享

QQ截图20230121151616

更多请看nodejs第四天资料

6. 数据库

  • 项目中操作mysql
npm i mysql
  • db.js
const mysql=require('mysql')
const db=mysql.createPool({
host:'127.0.0.1',
user:'root',
password:'root',
database:'my_db_01'
})
module.exports=db
  • 查询sql
const db=require('../db/index')
db.query('select * from users',(err,results)=>{
if(err){
return console.log(err.message)
}
console.log(results)
})
  • 新增sql
// 数据对象
const user={username:'zs',age:12}
// 定义sql
const sqlStr='insert into users(username,age) values (?,?)'
// 数组形式填充占位符
db.query(sqlStr,[user.username,user.age],(err,results)=>{
if(err){
return console.log(err.message)
}
if(results.affectedRows===1){
console.log('插入数据成功')
}
})

7. 身份认证

7 更多请看ppt

posted @   凌歆  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示