1234567891011121314151617181920212223242526272829303132333435363738 |
- var http = require("http");
- var url = require("url");
- var getData = require("./getData");
- var command = require("./command");
- /**
- * 参数
- * @type {Command}
- */
- var commandParams = command
- .version('0.0.1')
- .option('-p, --port <number>', 'change server port', 3000)//本地调试需改成24002
- .parse(process.argv.slice(2));
-
- var port = commandParams.port;
- console.log('Server starting...');
- function onRequest(request, response) {
- // 默认情况下,如果url路径中有中文,则会对中文进行URI编码,所以服务端要想获取中文需要对url进行URI解码
- // console.log(encodeURI(request.url));
- // 将第二个可选参数指定为 true, 表示将结果中的 query 解析为一个对象
- // url.parse 方法可以将一个 URL 路径解析为一个方便操作的对象
- var parseObj = url.parse(request.url, true);
-
- // 这里将解析属性挂载给 request 对象
- request.pathname = parseObj.pathname;
- request.query = parseObj.query;
- response.setHeader("Access-Control-Allow-Origin", "*"); // 设置可访问的源
- response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization,Content-Type,Access-Control-Allow-Origin"); // 设置允许的响应头
-
- getData(request, response);
- }
- http.createServer(onRequest).listen(port);
- console.log('Server started on port ' + port);
|