client side
made a request.js file what the 'withCredentials: true,' is most important thing to do. It allows browser to catch cookie with corss-domain request.
import axios from 'axios'
const service = axios.create({
baseURL: 'http://localhost:3000', // process.env.VUE_APP_BASE_API, // url = base url + request url
withCredentials: true, // send cookies when cross-domain requests
timeout: 5000 // request timeout
})
export default service
then you can make a real request in you file.
import request from '@/request'
export function events() {
return request({
url: '/api/event',
method: 'get'
})
}
server side
first of all you need install cors
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors({
credentials: true,
origin: ['http://localhost:3001'],//client side server address/must be
}));
CAUTION
origin: ['http://localhost:3001']
can not be
origin: '*'
the * will conflict with credentials : true
you have to assign it to your client side address