Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit b10a7fc

Browse files
authored
Merge pull request #15 from witoldsz/cors
CORS header and use semux.online when client launched from file #13
2 parents b51f59c + f83f63e commit b10a7fc

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

front/src/main/lib/api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export async function exec<T>(method: string, path: string): Promise<T> {
2-
const r = await fetch(path, { method })
2+
const { href } = window.location
3+
const prefix = /^https?/.test(href) ? '' : 'https://semux.online'
4+
const r = await fetch(prefix + path, { method })
35
try {
46
const { success, message, result } = await r.json()
57
return success ? result : Promise.reject(new Error(message))

server/src/main/app.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as express from 'express'
2+
import { Request, Response, NextFunction } from 'express'
23
import * as helmet from 'helmet'
34
import * as httpProxy from 'http-proxy'
45
import * as path from 'path'
@@ -17,16 +18,25 @@ async function main() {
1718
auth: `${user}:${pass}`,
1819
proxyTimeout: 5000,
1920
})
20-
const proxyMiddleware = (req, res, next) => {
21+
22+
const corsHeader = (res: Response) => res.header('Access-Control-Allow-Origin', '*')
23+
24+
const proxyMiddleware = (req: Request, res: Response, next: NextFunction) => {
25+
corsHeader(res)
2126
proxy.web(req, res, undefined, next)
2227
}
28+
2329
app.get('/v2.1.0/info', proxyMiddleware)
2430
app.get('/v2.1.0/account', proxyMiddleware)
2531
app.get('/v2.1.0/account/transactions', proxyMiddleware)
2632
app.get('/v2.1.0/account/pending-transactions', proxyMiddleware)
2733
app.get('/v2.1.0/account/votes', proxyMiddleware)
2834
app.get('/v2.1.0/delegates', proxyMiddleware)
2935
app.get('/v2.1.0/latest-block', proxyMiddleware)
36+
app.options('/v2.1.0/transaction/raw', (req, res) => {
37+
corsHeader(res)
38+
res.end()
39+
})
3040
app.post('/v2.1.0/transaction/raw', proxyMiddleware)
3141

3242
app.use((err, req, res, next) => {

0 commit comments

Comments
 (0)