技术更迭的太快,Node.js 和Go 一直想抽时间看看,最近看了些Node,得到的感想是各种语言都在努力构建自己的生态圈,想要一种语言吃遍天下。Javascript 这东西争议可能会比较大。
anyway ,记录下敲下来的命令。代码主要来自我看的书 First Week with Node.js
-
node
-
> function add(a, b, callback) {var result=a + b; callback(result); }
-
undefined
-
> add(2,3, function (c) { console.log('2 + 3 = ' +c)});
-
2 + 3 = 5
-
undefined
-
> add(1, 1, function(c) {console.log('Is 1 + 1 = 3? ' +(c==3));});
-
Is 1 + 1 = 3? false
-
undefined
-
> function doSomething (asyncCallback) { asyncCallback() ; }
-
undefined
-
> doSomething( function () { console.log('This runs synchronously.'); });
-
This runs synchronously.
-
undefined
-
> function doSomething2 (asyncCallback) {setTimeout(asyncCallback, Math.random() + 1000); }
-
undefined
-
> doSomething2 (function() { console.log('This runs asynchronously.'); });
-
undefined
-
> This runs asynchronously.
-
-
>
-
>
-
> doSomething2 (function() { console.log('This runs asynchronously.'); }); console.log('test');
-
test
-
undefined
-
> This runs asynchronously.
-
-
> dns.lookup('bing.com', function(err, address, family) { console.log('Address: ' +address +', Family: ' + family +', Err: ' + err); });
-
-
> Address: 204.79.197.200, Family: 4, Err: null
-
-
> var fs = require('fs')
-
undefined
-
> fs.writeFile('message.txt', 'Hello Node.js', function() { console.log('Hello Node.js ');}); console.log('Writing file...');
-
Writing file...
-
undefined
-
> Hello Node.js
-
-
> fs.readFile('message.txt', function(err, data) { console.log(data); });
-
undefined
-
> <Buffer 48 65 6c 6c 6f 20 4e 6f 64 65 2e 6a 73>
-
-
> fs.readFile('message.txt',{encoding: 'utf-8'} ,function(err, data) { console.log(data); });
-
undefined
-
> Hello Node.js
-
-
> var http = require('http');
-
undefined
-
> var server = http.createServer(function (request, response) {response.end('Hello Node.js'); });
-
undefined
-
> server.listen(8080);
-
-
> server.close();
-
-
> server = http.createServer(function (request, response) { response.end(request.headers['user-agent']); }); server.listen(8080);
-
-
> server.close()
-
-
> server = http.createServer(function (request, response) { Object.keys(request.headers).forEach(function (key) { response.write(key + ': '+request.headers[key]+ ' '); }); response.end(); });
-
-
> server.listen(8080);
-
-
> server.close()
-
-
> server = http.createServer(function (request, response) { Object.keys(request.headers).forEach(function (key) { response.write(key + ': '+request.headers[key]+ '\n'); }); response.end(); });
-
-
> server.listen(8080);
阅读(1592) | 评论(0) | 转发(0) |