From b13b4bc7c2af97e39a9fe873c4fe18ccacf8940c Mon Sep 17 00:00:00 2001 From: Naida Vatric Date: Tue, 18 Feb 2020 23:49:00 +0100 Subject: [PATCH] CORS error tryouts. --- app/views/publishPhotos.ejs | 5 +++-- index.js | 14 ++++++++++---- marketalarm-cors.json | 10 +++++++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/views/publishPhotos.ejs b/app/views/publishPhotos.ejs index e8cd4ff..24da85e 100644 --- a/app/views/publishPhotos.ejs +++ b/app/views/publishPhotos.ejs @@ -58,10 +58,10 @@ function uploadFile(file) { $("#status").html('Starting Upload...') url = c; - fetch(url, { + fetch(url, { method: 'PUT', headers: { - "content-type": "image/png" + "content-type": "image/*" }, mode: 'cors', body: file @@ -75,5 +75,6 @@ .catch(error => $("#status").html(error) ) .then(response => $("#status").html('File uploaded successfully: ' + filename + 'Response:' + response.text())); + } \ No newline at end of file diff --git a/index.js b/index.js index 9d6dbdd..8861c34 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,8 @@ const { const app = express(); +app.use(cors()); + app.use(forceSSL()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); @@ -36,8 +38,6 @@ app.use("/", routes); app.use("/assets", express.static("./app/public")); -app.use(cors()); - app.listen(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 version: "v2", //tried v4 also 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 }; const filename = req.query.filename; @@ -88,7 +89,12 @@ app.get("/generateSignedURL", (req, res) => { const [url] = await bucket.file(filename).getSignedUrl(options); 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); }); diff --git a/marketalarm-cors.json b/marketalarm-cors.json index 355e679..784c4e9 100644 --- a/marketalarm-cors.json +++ b/marketalarm-cors.json @@ -1,8 +1,12 @@ [ { - "origin": ["http://localhost"], - "responseHeader": ["Content-Type"], - "method": ["GET", "HEAD", "DELETE"], + "origin": ["*"], + "responseHeader": [ + "Content-Type", + "Access-Control-Allow-Origin", + "x-goog-resumable" + ], + "method": ["GET", "HEAD", "DELETE", "POST", "PUT", "OPTIONS"], "maxAgeSeconds": 3600 } ]