Commit eb5bcf29 authored by Stepheno's avatar Stepheno Committed by rockyl

(feat) proxy

parent 0cd5e26e
...@@ -17,7 +17,7 @@ async function execute() { ...@@ -17,7 +17,7 @@ async function execute() {
jsonServer.start({ jsonServer.start({
host: program.host, host: program.host,
port: program.port, port: program.port,
proxy: program.port, proxy: program.proxy,
folder: program.folder, folder: program.folder,
keyFile: program.keyFile, keyFile: program.keyFile,
certFile: program.certFile, certFile: program.certFile,
......
...@@ -8,10 +8,11 @@ const path = require('path'); ...@@ -8,10 +8,11 @@ const path = require('path');
const fs = require('fs'); const fs = require('fs');
const http = require('http'); const http = require('http');
const https = require('https'); const https = require('https');
const httpProxy = require('http-proxy');
const proxyServer = httpProxy.createProxyServer({});
let jsonPath; function handler(jsonPath, proxy) {
return function (request, response) {
function handler(request, response) {
const {url} = request; const {url} = request;
if (url === '/favicon.ico') { if (url === '/favicon.ico') {
...@@ -20,9 +21,25 @@ function handler(request, response) { ...@@ -20,9 +21,25 @@ function handler(request, response) {
return; return;
} }
const headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST,GET,PUT,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type,Access-Token',
};
if (proxy) {
const target = proxy.startsWith('http') ? proxy : `http://${proxy}`;
console.log(`proxy: ${target}${url}`)
Object.entries(headers).forEach(([key, val]) => response.setHeader(key, val));
return proxyServer.web(request, response, { target }, err => {
console.log(err);
})
}
const pos = url.indexOf('?'); const pos = url.indexOf('?');
const fileUri = pos < 0 ? url : url.substring(0, pos); const fileUri = pos < 0 ? url : url.substring(0, pos);
console.log(request.method, fileUri); console.log('mook: ', request.method, fileUri);
const filePath = path.join(jsonPath, fileUri); const filePath = path.join(jsonPath, fileUri);
let content, targetFile; let content, targetFile;
...@@ -39,12 +56,6 @@ function handler(request, response) { ...@@ -39,12 +56,6 @@ function handler(request, response) {
} }
} }
const headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST,GET,PUT,OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type,Access-Token',
};
if (content) { if (content) {
response.writeHead(200, headers); response.writeHead(200, headers);
...@@ -56,13 +67,13 @@ function handler(request, response) { ...@@ -56,13 +67,13 @@ function handler(request, response) {
})) }))
} }
response.end(); response.end();
}
} }
exports.start = function (options) { exports.start = function (options) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const {port, host, proxy, folder, keyFile, certFile} = options; const {port, host, proxy, folder, keyFile, certFile} = options;
const jsonPath = path.resolve(folder);
jsonPath = path.resolve(folder);
if (fs.existsSync(jsonPath)) { if (fs.existsSync(jsonPath)) {
let sslOpts; let sslOpts;
if(keyFile && certFile){ if(keyFile && certFile){
...@@ -77,7 +88,7 @@ exports.start = function (options) { ...@@ -77,7 +88,7 @@ exports.start = function (options) {
} }
} }
const server = sslOpts ? https.createServer(sslOpts, handler) : http.createServer(handler); const server = sslOpts ? https.createServer(sslOpts, handler(jsonPath, proxy)) : http.createServer(handler(jsonPath, proxy));
server.on('error', (err) => { server.on('error', (err) => {
reject(err.message); reject(err.message);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment