Skip to content

Commit b053777

Browse files
fixed cors restriction
1 parent 9b42f14 commit b053777

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

tasksync_backend/app.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const express = require('express')
22
const UserRouter = require('./router/UserRouter')
3-
const cors = require('cors');
3+
// const cors = require('cors');
44
const ProjectRouter = require('./router/ProjectRouter')
55
const TaskgroupRouter = require('./router/TaskgroupRouter')
66
const TaskRouter = require('./router/TaskRouter')
@@ -21,13 +21,24 @@ app.use(jsonParser)
2121
// cookie
2222
app.use(cookieParser())
2323

24+
app.all('*', function (req, res, next) {
25+
console.log(req.headers.origin)
26+
if (config.cors.origin.indexOf(req.headers.origin) !== -1){
27+
res.header("Access-Control-Allow-Origin", req.headers.origin)//访问的主机名称
28+
}
29+
res.header('Access-Control-Allow-Credentials','true'); //是否支持cookie跨域
30+
res.header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");
31+
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");//允许以下方法进行跨域请求
32+
next();
33+
});
34+
2435
// 全局配置跨域
25-
app.use(cors({
26-
origin: config.cors.origin,
27-
credentials: true,
28-
allowedHeaders: ['Content-Type'],
29-
methods: ['GET', 'POST', 'PUT', 'DELETE']
30-
}));
36+
// app.use(cors({
37+
// origin: config.cors.origin,
38+
// credentials: true,
39+
// allowedHeaders: ['Content-Type'],
40+
// methods: ['GET', 'POST', 'PUT', 'DELETE']
41+
// }));
3142

3243
app.use('/api/user', UserRouter)
3344
app.use('/api/project', ProjectRouter)

tasksync_backend/config/Config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ module.exports = {
1010
validTime: 3000
1111
},
1212
cors: {
13-
origin: ["http://tasksync.arkilovesprogramming.com:80", "https://tasksync.arkilovesprogramming.com:443"]
13+
origin: ["http://tasksync.arkilovesprogramming.com:80", "https://tasksync.arkilovesprogramming.com:443", "http://localhost:3000"],
1414
}
1515
}

tasksync_frontend/src/common/api.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
import axios from "axios"
22

33
// const base_url = "https://letschat.api.arkilovesprogramming.com:443/api"
4-
// const base_url = "http://localhost:9000/api"
5-
const base_url = "https://weiblog.arkilovesprogramming.com:444/api"
6-
7-
// Request interceptors for API calls
8-
// axios.interceptors.request.use(
9-
// config => {
10-
// config.headers['withCredentials'] = true;
11-
// return config;
12-
// },
13-
// error => {
14-
// return Promise.reject(error);
15-
// }
16-
// );
4+
const base_url = "http://localhost:9000/api"
5+
// const base_url = "https://weiblog.arkilovesprogramming.com:444/api"
176

187
// 修改 Axios 的默认配置
198
axios.defaults.withCredentials = true;

0 commit comments

Comments
 (0)