Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782002
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: JavaScript

2019-01-07 19:29:23

技术更迭的太快,Node.js 和Go 一直想抽时间看看,最近看了些Node,得到的感想是各种语言都在努力构建自己的生态圈,想要一种语言吃遍天下。Javascript 这东西争议可能会比较大。

anyway ,记录下敲下来的命令。代码主要来自我看的书 First Week with Node.js



点击(此处)折叠或打开

  1. node
  2. > function add(a, b, callback) {var result=a + b; callback(result); }
  3. undefined
  4. > add(2,3, function (c) { console.log('2 + 3 = ' +c)});
  5. 2 + 3 = 5
  6. undefined
  7. > add(1, 1, function(c) {console.log('Is 1 + 1 = 3? ' +(c==3));});
  8. Is 1 + 1 = 3? false
  9. undefined
  10. > function doSomething (asyncCallback) { asyncCallback() ; }
  11. undefined
  12. > doSomething( function () { console.log('This runs synchronously.'); });
  13. This runs synchronously.
  14. undefined
  15. > function doSomething2 (asyncCallback) {setTimeout(asyncCallback, Math.random() + 1000); }
  16. undefined
  17. > doSomething2 (function() { console.log('This runs asynchronously.'); });
  18. undefined
  19. > This runs asynchronously.

  20. >
  21. >
  22. > doSomething2 (function() { console.log('This runs asynchronously.'); }); console.log('test');
  23. test
  24. undefined
  25. > This runs asynchronously.

  26. > dns.lookup('bing.com', function(err, address, family) { console.log('Address: ' +address +', Family: ' + family +', Err: ' + err); });

  27. > Address: 204.79.197.200, Family: 4, Err: null

  28. > var fs = require('fs')
  29. undefined
  30. > fs.writeFile('message.txt', 'Hello Node.js', function() { console.log('Hello Node.js ');}); console.log('Writing file...');
  31. Writing file...
  32. undefined
  33. > Hello Node.js

  34. > fs.readFile('message.txt', function(err, data) { console.log(data); });
  35. undefined
  36. > <Buffer 48 65 6c 6c 6f 20 4e 6f 64 65 2e 6a 73>

  37. > fs.readFile('message.txt',{encoding: 'utf-8'} ,function(err, data) { console.log(data); });
  38. undefined
  39. > Hello Node.js

  40. > var http = require('http');
  41. undefined
  42. > var server = http.createServer(function (request, response) {response.end('Hello Node.js'); });
  43. undefined
  44. > server.listen(8080);

  45. > server.close();

  46. > server = http.createServer(function (request, response) { response.end(request.headers['user-agent']); }); server.listen(8080);

  47. > server.close()

  48. > server = http.createServer(function (request, response) { Object.keys(request.headers).forEach(function (key) { response.write(key + ': '+request.headers[key]+ ' '); }); response.end(); });

  49. > server.listen(8080);

  50. > server.close()

  51. > server = http.createServer(function (request, response) { Object.keys(request.headers).forEach(function (key) { response.write(key + ': '+request.headers[key]+ '\n'); }); response.end(); });

  52. > server.listen(8080);




阅读(1584) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~