From fb69bad2cf284fa7ad9696478721ab2e3ad19141 Mon Sep 17 00:00:00 2001 From: longalong <35618556+iamlongalong@users.noreply.github.com> Date: Sat, 16 Jul 2022 10:36:00 +0800 Subject: [PATCH] :sparkles: Feature: add server response headers for CORS (#939) Co-authored-by: long --- src/main/server/index.ts | 8 +++++++- src/main/server/utils.ts | 5 ++++- src/renderer/pages/Plugin.vue | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/server/index.ts b/src/main/server/index.ts index 7367391..ca3b653 100644 --- a/src/main/server/index.ts +++ b/src/main/server/index.ts @@ -39,13 +39,19 @@ class Server { } private handleRequest = (request: http.IncomingMessage, response: http.ServerResponse) => { + if (request.method === 'OPTIONS') { + handleResponse({ + response + }) + return + } + if (request.method === 'POST') { if (!routers.getHandler(request.url!)) { logger.warn(`[PicGo Server] don't support [${request.url}] url`) handleResponse({ response, statusCode: 404, - header: {}, body: { success: false } diff --git a/src/main/server/utils.ts b/src/main/server/utils.ts index b5009dd..7adf4c1 100644 --- a/src/main/server/utils.ts +++ b/src/main/server/utils.ts @@ -4,7 +4,10 @@ export const handleResponse = ({ response, statusCode = 200, header = { - 'Content-Type': 'application/json' + 'Content-Type': 'application/json', + 'access-control-allow-headers': '*', + 'access-control-allow-methods': 'POST, GET, OPTIONS', + 'access-control-allow-origin': '*' }, body = { success: false diff --git a/src/renderer/pages/Plugin.vue b/src/renderer/pages/Plugin.vue index 4ebaf4d..c53872c 100644 --- a/src/renderer/pages/Plugin.vue +++ b/src/renderer/pages/Plugin.vue @@ -140,7 +140,8 @@ export default class extends Vue { pluginNameList: string[] = [] loading = true needReload = false - pluginListToolTip = this.$T('PLUGIN_LIST')importLocalPluginToolTip = this.$T('PLUGIN_IMPORT_LOCAL') + pluginListToolTip = this.$T('PLUGIN_LIST') + importLocalPluginToolTip = this.$T('PLUGIN_IMPORT_LOCAL') id = '' os = '' defaultLogo: string = 'this.src="https://cdn.jsdelivr.net/gh/Molunerfinn/PicGo@dev/public/roundLogo.png"'