• angular8实现前端携带cookie发送给后端+nodejs获取前端发送的cookie


    1.前端测试代码angular8

    // 测试代码
      testCookie() {
        const url = 'http://10.11.11.11:3000/test/cookie'
        const params = {a:1}
        // {withCredentials: true} 这个是重点!!!!
        this.http.post( url,{}, params,  {withCredentials: true}).subscribe(_ => {
          console.log(_)
        })
      }
      // 测试代码
    

    2.后端测试代码nodejs

    app.js

    const express=require('express');
    // const bodyParser=require('body-parser');
    const session = require('express-session')
    var MySQLStore = require('express-mysql-session')(session);
     
    var options = {
        host: '22.22.22.22',
        port: 3306,
        user: 'root',
        password: '123',
        database: 'aaa'
    };
     
    var sessionStore = new MySQLStore(options);
    
    //引入路由模块
    const cors=require('cors');
    // const index=require("./routes/index");
    const test = require('./routers/test')
    
    //创建web服务器
    var server=express();
    //托管静态资源到public下;
    server.use(express.static('public'));
    //跨域请求cors
    server.use(cors
      (
        {
      // origin:"*" ,
      // origin:"http://localhost:8080" ,
      origin:"http://10.11.33.33:4200" ,
      // origin:"http://127.0.0.1:5500" ,
      credentials: true
    }
    )
    );
    // server.writeHead(200,{"Access-Control-Allow-Credentials":true});
    server.use(express.json())
    server.use(express.urlencoded({
      extended:false
    }));
    
    //session 启用会话中间件,用来保存用户登录状态以及验证码
    server.use(session({
      name: 'sessionId',
      resave: true,
      saveUninitialized: true,
      secret: 'APP_SESSION_SECRET',
      store: sessionStore // 将会话存到数据库
    }))
    
    
    
    
    
    
    server.use('/test',test);
    server.use('/index',index);
    
    server.use((req, res, next) => {
      res.status(404).send('404 not found')
    })
    
    // 异常处理器
    server.use((req,res,next,err)=>{
      console.log('err.message')
      console.log(err.message)
      // res.send(500);
    })
    
    server.listen(3000);
    
    

    ./routers/test.js

    const i18n = require('../i18n')
    //创建路由器对象
    const express = require('express')
    //引入连接池模块
    const pool = require('../pool.js')
    const querystring = require('querystring')
    const bodyParser = require('body-parser')
    //创建路由器对象
    var router = express.Router()
    var code200 = { code: 200, msg: 'successful' }
    var code401 = {
      code: 401,
      msg: 'this is required,so you cannot leave any of those blank ',
    }
    var code403 = { code: 403, msg: 'something has wrong' }
    //post提交需要三项,引入body-parser 中间件 然后.body
    router.use(
      bodyParser.urlencoded({
        extended: false,
      })
    )
    
    // 查询MC(Move Change)
    router.post('/cookie', function (req, res) {
      var obj = req.body
      var cooki = req.headers.cookie
      console.log('打印COOKIE')
      console.log(cooki)
      let data = {
        data: [1,2,1],
        msg: 'SearchMC Success',
        status: 'Success',
      }
    
      setTimeout(() => {
        res.send(data)
      }, 100)
    })
    //导出路由器对象
    module.exports = router
    
    

    3.开始发送请求,你会发现

    4.request headers里多了一个cookie字段,并且后端代码可以把前端发送的cookie打印出来了~~~~

  • 相关阅读:
    Uncaught (in promise) Error: Redirected when going from "/login" to "/home" via a navigation guard.
    开始写实际业务代码之前的一些准备工作
    vue 路由的基本配置
    Vue + Vue-router + Element-ui 搭建一个非常简单的dashboard demo
    vue报错 ModuleBuildError: Module build failed: Error: `sass-loader` requires `node-sass` >=4. Please i(Cannot find module ‘node-sass‘ 解决办法)
    vuex-mutations
    vuex及axios的get方法获取数据
    如何实现提交笔记-Markedown
    如何将线上项目下载至本地或者借鉴代码
    Sublime text3 vue代码格式化插件
  • 原文地址:https://www.cnblogs.com/sugartang/p/13853333.html
Copyright © 2020-2023  润新知