Commit c400a0fc authored by rockyl's avatar rockyl

增加http serve: game-cli serve -h -p -f

parent 75c77533
...@@ -3,9 +3,9 @@ const {exit,} = require('./tools'); ...@@ -3,9 +3,9 @@ const {exit,} = require('./tools');
const jsonServer = require('./json-server'); const jsonServer = require('./json-server');
program program
.option('-h, --host [string]', 'server host, default localhost') .option('-h, --host [string]', 'server host, default localhost', 'localhost')
.option('-p, --port [number]', 'server port, default 3000') .option('-p, --port [number]', 'server port, default 3000', 3000)
.option('-f, --folder [string]', 'folder of json files, default ./mock') .option('-f, --folder [string]', 'folder of json files, default ./', './')
.parse(process.argv); .parse(process.argv);
async function execute() { async function execute() {
......
const program = require('commander');
const {exit,} = require('./tools');
const httpServer = require('./http-server');
program
.option('-h, --host [string]', 'server host, default localhost', 'localhost')
.option('-p, --port [number]', 'server port, default 3001', 3001)
.option('-f, --folder [string]', 'folder of json files, default ./', './')
.parse(process.argv);
async function execute() {
console.log('Launching...');
httpServer.start({
host: program.host,
port: program.port,
folder: program.folder,
}).then(
({host, port, publicPath})=>{
console.log(`Http server start at http://${host}:${port}`);
console.log('Http path: ', publicPath);
},
(e)=>{
console.log(e);
}
)
}
execute().catch(e => {
exit(e);
});
/**
* Created by rockyl on 2018/9/18.
*
* http serve
*/
const path = require('path');
const fs = require('fs');
const handler = require('serve-handler');
const http = require('http');
exports.start = function (options) {
return new Promise((resolve, reject) => {
const {port, host, folder} = options;
const publicPath = path.resolve(folder);
if (fs.existsSync(publicPath)) {
const server = http.createServer((request, response) => {
return handler(request, response, {
public: publicPath,
});
});
server.on('error', (err) => {
reject(err.message);
});
server.listen(port, host, function () {
resolve({
host, port, publicPath,
});
});
} else {
reject('Public path is not exist: ' + publicPath)
}
})
};
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
"commander": "^2.18.0", "commander": "^2.18.0",
"fs-extra": "^6.0.1", "fs-extra": "^6.0.1",
"global-npm": "^0.3.0", "global-npm": "^0.3.0",
"progress": "^2.0.0" "progress": "^2.0.0",
"serve-handler": "^5.0.5"
} }
} }
This diff is collapsed.
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