Commit bc8844cb authored by 劳工's avatar 劳工

Merge branch 'dev' into 'master'

Dev

See merge request !2
parents 97cbe806 18d4b0fd
......@@ -5,6 +5,7 @@ const jsonServer = require('./json-server');
program
.option('-h, --host [string]', 'server host', 'localhost')
.option('-p, --port [number]', 'server port', 3000)
.option('--proxy [string]', 'server proxy address')
.option('-f, --folder [string]', 'folder of json files', './')
.option('-k, --key-file [string]', 'ssl key file')
.option('-c, --cert-file [string]', 'ssl cert file')
......@@ -16,6 +17,7 @@ async function execute() {
jsonServer.start({
host: program.host,
port: 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, folder, keyFile, certFile} = options;
jsonPath = path.resolve(folder);
const {port, host, proxy, folder, keyFile, certFile} = options;
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);
......
......@@ -3,6 +3,7 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
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