node.js 入门,

我最近因为工作原因,一直在用window系统,所以这个node.js我没有在linux系统下安装,我在window安装的,http://nodejs.org进去下载适合自己的版本

我感觉记住他的用法,之后是一个很方便的东西,它是一个异步式IO,节省多线程的开销

安装我就不提了,因为linux下面我没有安装,win的安装不值得提

控制台里面cmd,进入到node的安装文件夹:

安装成功:console控制台,这个在js里面也有使用。

node.js我刚看的时候,会看一下它protocol class or interfases:

http: http server,fs:File System,Global,Path,https,readline,URL,Timers

然后我去分别看这些里面的方法用法,感觉还不错啊

 

 

var http=require('http');
http.createServer(function(req,res){
    res.writeHead(200,{'Content-Type':'text/html'});
    res.write('<h1>Baby,come on!</h1>');
    res.end('<p>come on!</p>');
}).listen(3000);
console.log('http server is listening at port 3000');

 node.js是异步IO操作,我也可以这样认为,在整个程序内部,node.js是非阻塞的,

这样我们就要小心,我们前边的变量,用到后边,这样可能会是自己做的一个陷阱

 1 function readFileBack(err,data){
 2     if(err){
 3         console.error(err);
 4     }else{
 5         console.log(data);
 6     }
 7 }
 8 var fs=require('fs');
 9 fs.read('D:/test.txt','utf-8',readFileBack);
10 console.log('end');
11 
12 会先输出end,然后是内容

 

posted @ 2013-04-15 15:35  尹少爷  阅读(134)  评论(0编辑  收藏  举报