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

(feat) proxy

parent 0cd5e26e
......@@ -17,7 +17,7 @@ async function execute() {
jsonServer.start({
host: program.host,
port: program.port,
proxy: program.port,
proxy: program.proxy,
folder: program.folder,
keyFile: program.keyFile,
certFile: program.certFile,
......
......@@ -8,61 +8,72 @@ const path = require('path');
const fs = require('fs');
const http = require('http');
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) {
const {url} = request;
if (url === '/favicon.ico') {
response.writeHead(200);
response.end();
return;
}
const pos = url.indexOf('?');
const fileUri = pos < 0 ? url : url.substring(0, pos);
console.log(request.method, fileUri);
if (url === '/favicon.ico') {
response.writeHead(200);
response.end();
return;
}
const filePath = path.join(jsonPath, fileUri);
let content, targetFile;
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 (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();
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 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);
response.write(content);
} else {
response.writeHead(404, headers);
response.write(JSON.stringify({
'msg': `.json or /index.js not found, please check ${filePath}`,
}))
if (content) {
response.writeHead(200, headers);
response.write(content);
} else {
response.writeHead(404, headers);
response.write(JSON.stringify({
'msg': `.json or /index.js not found, please check ${filePath}`,
}))
}
response.end();
}
response.end();
}
exports.start = function (options) {
return new Promise((resolve, reject) => {
const {port, host, proxy, folder, keyFile, certFile} = options;
jsonPath = path.resolve(folder);
const jsonPath = path.resolve(folder);
if (fs.existsSync(jsonPath)) {
let sslOpts;
if(keyFile && certFile){
......@@ -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) => {
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