Node.js学习第四天
1. 基本使用
| 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)=>{ |
| |
| |
| res.send({name:'zs'}) |
| }) |
| |
| app.post('/user',(req,res)=>{ |
| |
| res.send('请求成功') |
| }) |
| app.get('/',(req,res)=>{ |
| |
| |
| console.log(req.query.name) |
| }) |
| 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')) |
| |
| |
2. nodemon
3. express 路由
| const express=require('express') |
| const router=express.Router() |
| |
| |
| router.get('/user/list',(req,res)=>{ |
| |
| }) |
| |
| |
| module.exports=router |
| 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.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. 跨域资源共享



更多请看nodejs第四天资料
6. 数据库

| const mysql=require('mysql') |
| |
| const db=mysql.createPool({ |
| host:'127.0.0.1', |
| user:'root', |
| password:'root', |
| database:'my_db_01' |
| }) |
| |
| module.exports=db |
| const db=require('../db/index') |
| |
| db.query('select * from users',(err,results)=>{ |
| |
| if(err){ |
| return console.log(err.message) |
| } |
| |
| console.log(results) |
| }) |
| |
| const user={username:'zs',age:12} |
| |
| |
| 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!