Commit 18d4b0fd authored by Stepheno's avatar Stepheno

(feat) proxy

parent 3d6ddb62
...@@ -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,61 +8,72 @@ const path = require('path'); ...@@ -8,61 +8,72 @@ 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) {
const {url} = request;
function handler(request, response) { if (url === '/favicon.ico') {
const {url} = request; response.writeHead(200);
response.end();
if (url === '/favicon.ico') { return;
response.writeHead(200); }
response.end();
return;
}
const pos = url.indexOf('?');
const fileUri = pos < 0 ? url : url.substring(0, pos);
console.log(request.method, fileUri);
const filePath = path.join(jsonPath, fileUri); const headers = {
let content, targetFile; '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 (fs.existsSync(targetFile = filePath + '.json')) { if (proxy) {
content = fs.readFileSync(targetFile, 'utf-8'); const target = proxy.startsWith('http') ? proxy : `http://${proxy}`;
} else if (fs.existsSync(targetFile = filePath + '/index.js')) { console.log(`proxy: ${target}${url}`)
try { Object.entries(headers).forEach(([key, val]) => response.setHeader(key, val));
const jsonObject = require(targetFile)(); return proxyServer.web(request, response, { target }, err => {
content = JSON.stringify(jsonObject); console.log(err);
} catch (e) { })
console.log(e); }
content = e.toString();
const pos = url.indexOf('?');
const fileUri = pos < 0 ? url : url.substring(0, pos);
console.log('mook: ', request.method, fileUri);
const filePath = path.join(jsonPath, fileUri);
let content, targetFile;
if (fs.existsSync(targetFile = filePath + '.json')) {
content = fs.readFileSync(targetFile, 'utf-8');
} else if (fs.existsSync(targetFile = filePath + '/index.js')) {
try {
const jsonObject = require(targetFile)();
content = JSON.stringify(jsonObject);
} catch (e) {
console.log(e);
content = e.toString();
}
} }
}
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) {
response.writeHead(200, headers); if (content) {
response.write(content); response.writeHead(200, headers);
} else { response.write(content);
response.writeHead(404, headers); } else {
response.write(JSON.stringify({ response.writeHead(404, headers);
'msg': `.json or /index.js not found, please check ${filePath}`, response.write(JSON.stringify({
})) 'msg': `.json or /index.js not found, please check ${filePath}`,
}))
}
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