码迷,mamicode.com
首页 > Web开发 > 详细

node.js教程learnyounode 闯关答案

时间:2015-07-22 14:32:11      阅读:359      评论:0      收藏:0      [点我收藏+]

标签:

  1. #node.js教程learnyounode 闯关答案
  2. #ex1
  3. console.log("HELLO WORLD");
  4. #ex2
  5. var sum =0;
  6. for(var i =2;i<process.argv.length;i+=){
  7. sum+=parseFloat(process.argv[i]);
  8. }
  9. console.log(sum);
  10. #ex3
  11. var fs = require(‘fs‘);
  12. var pathname = process.argv[2];
  13. //console.log(pathname);
  14. var buf = fs.readFileSync(pathname);
  15. var results = buf.toString().split("\n");
  16. //console.log(results);
  17. console.log(results.length-1);
  18. #ex4
  19. var fs = require(‘fs‘);
  20. var pathname = process.argv[2];
  21. fs.readFile(pathname,handle);
  22. function handle(err,data){
  23. if(err){
  24. console.error(err);
  25. return;
  26. }
  27. var results = data.toString().split("\n");
  28. console.log(results.length-1);
  29. }
  30. #ex5
  31. var fs = require(‘fs‘);
  32. var path = require(‘path‘);
  33. var pathname = process.argv[2];
  34. var extname = process.argv[3];
  35. fs.readdir(pathname,function(err,files){
  36. for(var i=0;i<files.length;i++){
  37. if("."+extname == path.extname(files[i])){
  38. console.log(files[i]);
  39. }
  40. }
  41. });
  42. #ex6
  43. ##program.js
  44. var mymodule = require("./mymodule");
  45. var pathname = process.argv[2];
  46. var extname = process.argv[3];
  47. mymodule(pathname,extname,callback);
  48. function callback(err,data){
  49. if(err){
  50. console.error(err);
  51. return ;
  52. }
  53. data.forEach(function(file){
  54. console.log(file);
  55. });
  56. }
  57. ##mymodule.js
  58. var fs = require(‘fs‘);
  59. var path = require(‘path‘);
  60. module.exports = function (pathname,extname,callback){
  61. var results = new Array();
  62. var cnt =0;
  63. fs.readdir(pathname,function(err,files){
  64. if(err){
  65. return callback(err);
  66. }
  67. for(var i=0;i<files.length;i++){
  68. if("."+extname == path.extname(files[i])){
  69. results[cnt++] = files[i];
  70. }
  71. }
  72. //console.log(results);
  73. callback(null,results);
  74. });
  75. };
  76. #ex7
  77. var http = require("http");
  78. var url = process.argv[2];
  79. http.get(url, function(res) {
  80. res.setEncoding("utf-8");
  81. res.on("data",function(data){
  82. console.log(data);
  83. });
  84. });
  85. #ex8
  86. var http = require("http");
  87. var url = process.argv[2];
  88. http.get(url, function(res) {
  89. res.setEncoding("utf-8");
  90. var results ="";
  91. res.on("data",function(data){
  92. results +=data;
  93. });
  94. res.on("end",function(){
  95. console.log(results.length);
  96. console.log(results);
  97. });
  98. });
  99. #ex9
  100. #solution1:
  101. var http = require("http");
  102. http.get(process.argv[2],handle1);
  103. http.get(process.argv[3],handle2);
  104. http.get(process.argv[4],handle3);
  105. var results= ["","",""];
  106. function handle1(res){
  107. res.setEncoding="utf-8";
  108. res.on("data",function(data){
  109. results[0] +=data;
  110. });
  111. res.on("end",function(){
  112. //console.log(results[0]);
  113. });
  114. }
  115. function handle2(res){
  116. res.setEncoding="utf-8";
  117. res.on("data",function(data){
  118. results[1] +=data;
  119. });
  120. res.on("end",function(){
  121. //console.log(results[1].toString());
  122. });
  123. }
  124. function handle3(res){
  125. res.setEncoding="utf-8";
  126. res.on("data",function(data){
  127. results[2] +=data;
  128. });
  129. res.on("end",function(){
  130. console.log(results[0].toString());
  131. console.log(results[1].toString());
  132. console.log(results[2].toString());
  133. });
  134. }
  135. #solution2:
  136. var http = require("http");
  137. var results=[];
  138. var count =0;
  139. for(var i=0;i<3;i++){
  140. httpGet(i);
  141. }
  142. function httpGet(index){
  143. http.get(process.argv[index+2],function(res){
  144. res.setEncoding="utf-8";
  145. results[index]="";
  146. res.on("data",function(data){
  147. results[index] +=data;
  148. });
  149. res.on("end",function(){
  150. count++;
  151. if(count>=3){
  152. for(var i=0;i<results.length;i++){
  153. console.log(results[i]);
  154. }
  155. }
  156. });
  157. });
  158. }
  159. ##参考答案
  160. var http = require(‘http‘)
  161. var bl = require(‘bl‘)
  162. var results = []
  163. var count = 0
  164. function printResults () {
  165. for (var i = 0; i < 3; i++)
  166. console.log(results[i])
  167. }
  168. function httpGet (index) {
  169. http.get(process.argv[2 + index], function (response) {
  170. response.pipe(bl(function (err, data) {
  171. if (err)
  172. return console.error(err)
  173. results[index] = data.toString()
  174. count++
  175. if (count == 3)
  176. printResults()
  177. }))
  178. })
  179. }
  180. for (var i = 0; i < 3; i++)
  181. httpGet(i)
  182. #ex10
  183. var net = require("net");
  184. var port = process.argv[2];
  185. var server = net.createServer(function(socket){
  186. socket.end(getTime()+"\n");
  187. });
  188. server.listen(port);
  189. console.log("Server started http://127.0.0.1:%s",port)
  190. function getTime(){
  191. var date = new Date();
  192. var year = date.getFullYear();
  193. var month = justify(date.getMonth()+1);
  194. var day = justify(date.getDate());
  195. var hour = justify(date.getHours());
  196. var minute = justify(date.getMinutes());
  197. return year + "-"+month+"-"+day+" "+hour+":"+minute;
  198. }
  199. function justify(str){
  200. return str<10?"0"+str:str;
  201. }
  202. #ex11
  203. var http = require("http");
  204. var fs = require("fs");
  205. var server = http.createServer(function(req,res){
  206. res.writeHead(200,{"content-type":"text/plain"});
  207. fs.createReadStream(process.argv[3]).pipe(res);
  208. });
  209. server.listen(process.argv[2]);
  210. #ex12
  211. var http = require("http");
  212. http.createServer(function(req,res){
  213. var post = "";
  214. req.on(‘data‘,function(chunk){
  215. post += chunk;
  216. });
  217. req.on(‘end‘,function(){
  218. post = post.toUpperCase();
  219. res.end(post);
  220. });
  221. }).listen(process.argv[2]);
  222. #参考答案:
  223. var http = require(‘http‘)
  224. var map = require(‘through2-map‘)
  225. var server = http.createServer(function (req, res) {
  226. if (req.method != ‘POST‘)
  227. return res.end(‘send me a POST\n‘)
  228. req.pipe(map(function (chunk) {
  229. return chunk.toString().toUpperCase()
  230. })).pipe(res)
  231. })
  232. server.listen(Number(process.argv[2]))
  233. #ex13
  234. var http = require("http");
  235. var url = require("url");
  236. http.createServer(function(req,res){
  237. var url2 = url.parse(req.url,true);
  238. var pathname = url2.pathname;
  239. var iso = url2.query.iso;
  240. var date = new Date(iso);
  241. var result;
  242. res.writeHead(200,{"Content-Type":"application/json"});
  243. if(pathname == "/api/parsetime"){
  244. result = {
  245. "hour":date.getHours(),
  246. "minute":date.getMinutes(),
  247. "second":date.getSeconds()
  248. };
  249. res.end(JSON.stringify(result));
  250. }else if(pathname == "/api/unixtime"){
  251. result = {
  252. "unixtime":date.getTime()
  253. };
  254. res.end(JSON.stringify(result));
  255. }
  256. }).listen(process.argv[2]);





node.js教程learnyounode 闯关答案

标签:

原文地址:http://www.cnblogs.com/homer3000/p/4667322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!