const http = require('http');
const { program } = require('commander');

program
  .option('-p, --port <number>', 'port number', '3000')  // 第三个参数是默认值
  .option('-o, --host <string>', 'host name', 'localhost')
  .parse(process.argv);


// 创建服务器
const server = http.createServer((req, res) => {
  // 设置响应头
  res.writeHead(200, {'Content-Type': 'text/plain'});
  
  // 发送响应数据
  res.end('Hello, World!\n');
});

const options = program.opts();
// 监听端口
server.listen(options.port, options.host, () => {
  console.log(`Server running at http://${options.host}:${options.port}/`);
});