CORS error tryouts.

This commit is contained in:
Naida Vatric
2020-02-18 23:49:00 +01:00
parent c11248e100
commit b13b4bc7c2
3 changed files with 20 additions and 9 deletions

View File

@@ -58,10 +58,10 @@
function uploadFile(file) { function uploadFile(file) {
$("#status").html('Starting Upload...') $("#status").html('Starting Upload...')
url = c; url = c;
fetch(url, { fetch(url, {
method: 'PUT', method: 'PUT',
headers: { headers: {
"content-type": "image/png" "content-type": "image/*"
}, },
mode: 'cors', mode: 'cors',
body: file body: file
@@ -75,5 +75,6 @@
.catch(error => $("#status").html(error) .catch(error => $("#status").html(error)
) )
.then(response => $("#status").html('File uploaded successfully: ' + filename + 'Response:' + response.text())); .then(response => $("#status").html('File uploaded successfully: ' + filename + 'Response:' + response.text()));
} }
</script> </script>

View File

@@ -23,6 +23,8 @@ const {
const app = express(); const app = express();
app.use(cors());
app.use(forceSSL()); app.use(forceSSL());
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
@@ -36,8 +38,6 @@ app.use("/", routes);
app.use("/assets", express.static("./app/public")); app.use("/assets", express.static("./app/public"));
app.use(cors());
app.listen(APP_PORT, () => app.listen(APP_PORT, () =>
console.log(`Example app listening on port ${APP_PORT}!`) console.log(`Example app listening on port ${APP_PORT}!`)
); );
@@ -77,7 +77,8 @@ app.get("/generateSignedURL", (req, res) => {
//Tried to define Google ID and private key while debugging //Tried to define Google ID and private key while debugging
version: "v2", //tried v4 also version: "v2", //tried v4 also
action: "write", action: "write",
contentType: "image/*", //tried without and with specific image/png ex. "Content-Type": "image/*",
//contentType: "image/*", //tried without and with specific image/png ex.
expires: Date.now() + 86400000 expires: Date.now() + 86400000
}; };
const filename = req.query.filename; const filename = req.query.filename;
@@ -88,7 +89,12 @@ app.get("/generateSignedURL", (req, res) => {
const [url] = await bucket.file(filename).getSignedUrl(options); const [url] = await bucket.file(filename).getSignedUrl(options);
console.log(`The signed url is ${url}.`); console.log(`The signed url is ${url}.`);
res.send(url);
res.header("Access-Control-Allow-Origin", "http://localhost:5000");
res.header("Access-Control-Allow-Methods", "PUT,OPTIONS");
res.header("Access-Control-Allow-Headers", "Content-Type, Origin");
res.header("Access-Control-Max-Age", "86400000");
res.status(200).send(url);
} }
generateSignedUrl().catch(console.error); generateSignedUrl().catch(console.error);
}); });

View File

@@ -1,8 +1,12 @@
[ [
{ {
"origin": ["http://localhost"], "origin": ["*"],
"responseHeader": ["Content-Type"], "responseHeader": [
"method": ["GET", "HEAD", "DELETE"], "Content-Type",
"Access-Control-Allow-Origin",
"x-goog-resumable"
],
"method": ["GET", "HEAD", "DELETE", "POST", "PUT", "OPTIONS"],
"maxAgeSeconds": 3600 "maxAgeSeconds": 3600
} }
] ]