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.开始发送请求,你会发现