axios基本使用,express中间件

axios基本使用

axios常用属性

  • url:用于请求的服务器url

    url:'/url'
    
  • method: 创建时使用的方法

    //默认为get
    
    method:'get'
    
  • transformRequest: 允许在请求数据发送到服务器之前对其进行更改

transformRequest:[function(data){
    
    
	//做任何想要的数据转化
	//然后返回  return data
}]
  • transformResponse: 允许在 then/catch 之前对相应数据进行修改

    transformResponse: [function(data) {
          
          
        /做任何想要的数据转化
    	//然后返回  return data
    }]
    
  • params: 是即将与请求一起发送的url参数;必须是纯对象或 URLSearchParams对象,请求参数拼接在URL上。

    params:{
          
          
    	ID:1234565
    }
    
  • data: 作为请求主体发送的数据

    //仅适用于 请求方法“get” "post" "patch
    
    data: {
          
          
      firstName: ’zxzx’
    }
    

axios请求

Get请求

后端发送的json数据,显示到浏览器上

axios.get('/login',{
    
    
	params: {
    
    
		pageNum:2,
		pageSize:3
	}
})
	.then(function (response){
    
    
		console.log(response)
	})
	.catch(function (error){
    
    
		console.log(error)
	})
Post请求

使用post请求传递对象数据

axios.post('http://127.0.0.1:3000/user',{
	username:123,
	password:2345
})
	.then(function (response){
		console.log(response)
	})
	.catch(function (error){
		console.log(error)
	})
axios({
    
    
	method:'post',
	url:'/user',
	data: {
    
    
		firstname: 'welcome',
		lastname:'coming'
	}
})
//获取远端图片
axios({
    
    
	method:'post',
	url:'http://127.0.0.1:3567',
	responseType:'stream'
})
//responseType`表示服务器响应的数据类型,可以是 ‘arraybuffer’, ‘blob’, ‘document’, ‘json’, ‘text’, ‘stream’
.then(function(response) {
    
    
	response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
});

express中间件

  • 内置中间件

// 托管静态文件

//4.内置中间件

app.use(express.static(‘static’))//访问static目录下的文件 127.0.0.1:3000/css/base.css

  • 应用级中间件(用于权限判断)

    app.use((req,res,next)=>{

    console.log(new Date());

    next()//表示匹配完成这个中间件后程序继续向后执行

    })

  • 路由级中间件 (少见)

    app.get(‘/article/add’,(req,res,next)=>{

    console.log(‘执行郑家新闻’);

    next()

    })

    app.get(‘/article/:id’,(req,res)=>{ //主要用于修改数据

    var id = req.params[“id”]

    res.send(“动态路由”**+**id)

    // //http://127.0.0.1:3000/article/xxx

    })

  • 错误形中间件

    // 放在最后

    app.use((req,res,next)=>{

    res.status(404).send(“404”)// 匹配所有网址未找到定义的网址时 匹配到报错

    })

  • 第三方中间件
    const bodyParser = require('body-parser')
    //配置第三方中间件  接受post表单
    app.use(bodyParser.urlencoded({
          
           extended: false }))
    app.use(bodyParser.json())
    

    session中间件

//配置session中间件
app.use(session({
    
    
    secret: 'this is session',//服务端生成session时的签名
    name:'xxx',//设置cookie名称
    resave: false,//强制保存  session 即使没用变化
    saveUninitialized: true,//强制将未初始化的session存储
    //可以来设置cookie   session是基于cookie
    cookie: {
    
     
        maxAge:1000*60,
        secure: false,//true表示只有https协议才能访问cookie

     },
     rolling:true, //在每次请求时强行设置cookie ,重置cookie过期时间  默认false
     store:MongoStore.create({
    
    
        mongoUrl: 'mongodb://admin:[email protected]:27017/admin',
        touchAfter:24*3600//不管发出了多少请求, 在24小时内只更新一次session  除非改变session
      })
  }))

app.get('/',(req,res)=>{
    
    
    //获取session
    if(req.session.username){
    
    
        res.send(req.session.username+'登录')
    }else{
    
    
        res.send('没有登陆')
    }
 
    
})

app.get("/login",(req,res)=>{
    
    
    //设置session
    req.session.username="hk"
    res.send("首页")
})

app.get("/loginOut",(req,res)=>{
    
    
    //1. 设置session 的过期时间 为0 (他会把所有的session都销毁)
    // req.session.cookie.maxAge=0

    //2.销毁指定session
    // req.session.username=""

    //3.销毁session destroy
    req.session.destroy()

    res.send("退出登录")
})


猜你喜欢

转载自blog.csdn.net/m0_63338686/article/details/124646645