CORS策略阻止了我在谷歌云平台应用引擎中的API请求



我将我的node js服务器上传到谷歌云应用程序引擎服务,以便能够使前端工作。我也上传前端和我的网站现在是生活。

node js运行良好,部署在google cloud的app引擎中。

我唯一的问题是,每当我试图提出请求时,我都会被cors策略阻止,我到处寻找解决方案,但无法找到任何解决方案。

下面是在我的控制台中出现的错误:

Access to XMLHttpRequest at 'https://vocal-byte-324123.de.r.appspot.com/login' 
from origin 'https://mywebiste.org' has been blocked by CORS policy: The value of the 
'Access-Control-Allow-Origin' header in the response must not be the wildcard '*'
when the request's credentials mode is 'include'. The credentials mode of requests 
initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

我应该怎么做才能摆脱这个错误?

这是我正在做的

index.js from my node js server

require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const mysql = require('mysql');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const multer = require('multer');
const path = require('path');
const request = require('request');
const app = express();
const db = mysql.createPool({
host: "127.0.0.1",
user: "xxx",
password: "xxx",
database:"xxx" 
});
//Middleware 
app.use(cors({
origin:["https://mywebsite.org"],
method:["GET","POST","OPTIONS","PUT"],
credentials: true,
}));
app.options('*', cors());
app.use(express.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(session({
key: "userID",
secret: "",
resave: false,
saveUninitialized: false,
cookie: {
expires: 3600 * 1000 * 24 * 365 * 10,
},
}))
app.use('/', express.static(path.join(__dirname, '/')));
//Log In User API
app.post('/api/login', (req, res) => {
const email = req.body.email
const password = req.body.password
db.query("SELECT * FROM users WHERE email = ? and password = ?", [email,password], (err, result) => {
if(err){
console.log(err);
res.send({err:err});
}
if(result.length > 0){

req.session.user = result;

res.send(result)
}else{
res.send({message:"Wrong username or password"})
}
});
});
//Fetch Logged in User Info, and Save a Session
app.get("/login", (req, res) => {
if(req.session.user){
res.send({loggedIn: true, user: req.session.user})
}else{
res.send({loggedIn: false})
}
})

//Start the Server
app.listen(8080,() => {console.log("running on port 3001")});

在前端,我是这样请求的:

// this is the URL of where my node js server lies https://vocal-byte-324123.de.r.appspot.com
const loginUser = () => {

Axios.post('https://vocal-byte-324123.de.r.appspot.com/api/login',{
email: email, 
password:password,
}).then((response) => {

if(response.data.message){
alert("Incorrect email or password");
}else{
//  alert("Logged In Successfully");
history.push("/Home");
window.location.reload();
}   
});
}

请帮忙好吗?编辑:

试了试,回答不太好。

在你的路由上试试这个中间件…

app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"OPTIONS, GET, POST, PUT, PATCH, DELETE"
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
next();
});

希望这对你有用。

相关内容

  • 没有找到相关文章

最新更新