proxy for cors

This commit is contained in:
cupcakearmy 2021-12-20 18:14:59 +01:00
parent d0f83e6148
commit ba38d2b819
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
5 changed files with 47 additions and 5 deletions

View File

@ -122,7 +122,7 @@ Running `npm run dev` in the root folder will start the following things
- rust backend with hot reload - rust backend with hot reload
- client with hot reload - client with hot reload
You can see the app under [localhost:5000](http://localhost:5000). You can see the app under [localhost:1234](http://localhost:1234).
###### Attributions ###### Attributions

View File

@ -1,5 +1,3 @@
import { dev } from '$app/env'
export type Note = { export type Note = {
contents: string contents: string
views?: number views?: number
@ -13,9 +11,9 @@ type CallOptions = {
method: string method: string
body?: any body?: any
} }
const base = dev ? 'http://localhost:5000/api/' : '/api/'
async function call(options: CallOptions) { async function call(options: CallOptions) {
return fetch(base + options.url, { return fetch('/api/' + options.url, {
method: options.method, method: options.method,
body: options.body === undefined ? undefined : JSON.stringify(options.body), body: options.body === undefined ? undefined : JSON.stringify(options.body),
mode: 'cors', mode: 'cors',

View File

@ -3,9 +3,11 @@
"dev:docker": "docker-compose up memcached", "dev:docker": "docker-compose up memcached",
"dev:backend": "cargo watch -x 'run --bin cryptgeon'", "dev:backend": "cargo watch -x 'run --bin cryptgeon'",
"dev:front": "pnpm --prefix client run dev", "dev:front": "pnpm --prefix client run dev",
"dev:proxy": "node proxy.mjs",
"dev": "run-p dev:*" "dev": "run-p dev:*"
}, },
"devDependencies": { "devDependencies": {
"http-proxy": "^1.18.1",
"npm-run-all": "^4.1.5" "npm-run-all": "^4.1.5"
} }
} }

31
pnpm-lock.yaml generated
View File

@ -1,9 +1,11 @@
lockfileVersion: 5.3 lockfileVersion: 5.3
specifiers: specifiers:
http-proxy: ^1.18.1
npm-run-all: ^4.1.5 npm-run-all: ^4.1.5
devDependencies: devDependencies:
http-proxy: 1.18.1
npm-run-all: 4.1.5 npm-run-all: 4.1.5
packages: packages:
@ -116,6 +118,20 @@ packages:
engines: {node: '>=0.8.0'} engines: {node: '>=0.8.0'}
dev: true dev: true
/eventemitter3/4.0.7:
resolution: {integrity: sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==}
dev: true
/follow-redirects/1.14.6:
resolution: {integrity: sha512-fhUl5EwSJbbl8AR+uYL2KQDxLkdSjZGR36xy46AO7cOMTrCMON6Sa28FmAnC2tRTDbd/Uuzz3aJBv7EBN7JH8A==}
engines: {node: '>=4.0'}
peerDependencies:
debug: '*'
peerDependenciesMeta:
debug:
optional: true
dev: true
/function-bind/1.1.1: /function-bind/1.1.1:
resolution: {integrity: sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==} resolution: {integrity: sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==}
dev: true dev: true
@ -157,6 +173,17 @@ packages:
resolution: {integrity: sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==} resolution: {integrity: sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==}
dev: true dev: true
/http-proxy/1.18.1:
resolution: {integrity: sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==}
engines: {node: '>=8.0.0'}
dependencies:
eventemitter3: 4.0.7
follow-redirects: 1.14.6
requires-port: 1.0.0
transitivePeerDependencies:
- debug
dev: true
/is-arrayish/0.2.1: /is-arrayish/0.2.1:
resolution: {integrity: sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=} resolution: {integrity: sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=}
dev: true dev: true
@ -339,6 +366,10 @@ packages:
path-type: 3.0.0 path-type: 3.0.0
dev: true dev: true
/requires-port/1.0.0:
resolution: {integrity: sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=}
dev: true
/resolve/1.20.0: /resolve/1.20.0:
resolution: {integrity: sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A==} resolution: {integrity: sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A==}
dependencies: dependencies:

11
proxy.mjs Normal file
View File

@ -0,0 +1,11 @@
import httpProxy from 'http-proxy'
import http from 'http'
const proxy = httpProxy.createProxyServer({})
var server = http.createServer(function (req, res) {
const target = req.url.startsWith('/api/') ? 'http://localhost:5000' : 'http://localhost:3000'
proxy.web(req, res, { target })
})
server.listen(1234)