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');
const jsonServer = require('./json-server');
program
.option('-h, --host [string]', 'server host, default localhost')
.option('-p, --port [number]', 'server port, default 3000')
.option('-f, --folder [string]', 'folder of json files, default ./mock')
.option('-h, --host [string]', 'server host, default localhost', 'localhost')
.option('-p, --port [number]', 'server port, default 3000', 3000)
.option('-f, --folder [string]', 'folder of json files, default ./', './')
.parse(process.argv);
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)
}
})
};
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