Home > Article > Web Front-end > nodejs preliminary experience_node.js
Foreword: The origin of writing this article:
1. Some time ago, a new project was launched in the unit. The work to be done on the server side was not much and not too troublesome. It was just dealing with some middle-tier services. Moreover, everyone in our team knew JavaScript, so the leader decided to try the server side. JavaScript, I was fortunate enough to be sent to study Node for a few days, and started the chapter of node.js with a feeling of chicken jelly. This article is a summary of these days of research.
2. If a JavaScript engineer has never heard of node.js, then I think you have missed something. Every excellent front-end engineer needs to understand the background processing process, so if you can start from JavaScript, wouldn’t it? What a wonderful thing.
3. The popularity of the Internet has made JavaScript infinitely popular, and server-side JavaScript is not a new technology. There are many related frameworks, but the success of node.js has made it explosively appear in our sight. Many front-end engineers have seen another hope of writing from front-end to back-end. Note: node.js is a framework that allows developers to write server-side code using the JavaScript language.
4. In August this year, I was asked about Node.js in the final (fifth round) interview of a large company. The corresponding answer was quite bad. You know the result. I feel like this The problem was one of the key points that led to the failure... That company was a company I had longed for when I was in college. Looking back on that experience and process now, I can’t even say I feel regretful. After all, I really tried my best - in fact This article is more about completing a small knot of my heart...Okay, I'm going off topic again.
5. All reprints are welcome, but please indicate the source, thank you.
What is Node?
When writing something, I’d better try to be as comprehensive as possible, so I also selectively picked up some basic concepts from the Internet, and changed some places to suit my own understanding. It’s okay to have some understanding of these conceptual things. Choose to skip this section.
1. Node is a server-side JavaScript interpreter, but if you really think that students who are good at JavaScript can easily master it by learning Node, then you are wrong. Summary: I don’t know whether the water is deep or not, but it is indeed not shallow. .
2. The goal of Node is to help programmers build highly scalable applications and write connection code that can handle tens of thousands of simultaneous connections to a physical machine. Handling high concurrency and asynchronous I/O is one of the reasons why Node attracts developers' attention.
3. Node itself runs the Google V8 JavaScript engine, so the speed and performance are very good, as you can see by looking at chrome, and while Node encapsulates it, it also improves its ability to process binary data. Therefore, Node not only simply uses V8, but also optimizes it to make it more powerful in various environments. (What is the V8 JavaScript engine? Please "Baidu knows")
4. Third-party extensions and modules play an important role in the use of Node. The following will also introduce downloading npm. npm is a module management tool. Use it to install various Node software packages (such as express, redis, etc.) and publish the software packages you write for Node.
Install Node
Here I will briefly talk about installing Node in both window7 and linux environments. Be sure to pay attention to the Python version when installing. The installation has failed many times due to problems with the Python version. It is recommended to use version 2.6. Lower versions will cause Node installation errors. To query the Python version, enter: pyhton -v
in the terminal.1. Let’s first introduce the installation under Linux. Node is very convenient to install and use in Linux environment. It is recommended to run Node under Linux, ^_^...I am using Ubuntu11.04
a. Install dependent packages: 50-100kb/s, each package can be downloaded and installed in about one minute
sudo apt-get install g++ curl libssl-dev apache2-utils sudo apt-get install git-core
b. Run the commands step by step in the terminal:
git clone git://github.com/joyent/node.git cd node ./configure make sudo make install
安装顺利的话到这一步Node就算安装成功了,2M的网络用了共计12分钟。
注:如果不用git下载也可以直接下载源码,不过这样下载安装需要注意Node版本问题。使用git下载安装是最方便的,所以推荐之。
2.在Windows下使用Cygwin安装Node,这个方式不太推荐,因为真的需要较长时间和较好的人品。我的系统是 win7旗舰版
Cygwin是一个在windows平台上运行的unix模拟环境,下载地址:http://cygwin.com/setup.exe。
下载好Cygwin后开始安装,步骤:
a.选择下载的来源 - Install from Internet
b.选择下载安装的根目录
c.选择下载文件所存放的目录
d.选择连接的方式
e.选择下载的网站 - http://mirrors.163.com/cygwin
f.麻烦就麻烦在这步,考验人品的时候到了。需要的下载安装时间不确定,反正需要比较长的时间(超过20分钟),偶尔会出现安装失败的情况。单击一下各个程序包前面的旋转箭头图标选择你想要的版本,选中时会出现了"x"号表示已经选中了该程序包。选择需要下载的程序包:
Devel包:
gcc-g++: C++ compiler
gcc-mingw-g++: Mingw32 support headers and libraries for GCC C++
gcc4-g++: G++ subpackage
git: Fast Version Control System – core files
make: The GNU version of the ‘make' utility
openssl-devel: The OpenSSL development environment
pkg-config: A utility used to retrieve information about installed libraries
zlib-devel: The zlib compression/decompression library (development)
Editor包:vim: Vi IMproved – enhanced vi editor
Python包:把Default切换成install状态即可
Web包:
wget: Utility to retrieve files from the WWW via HTTP and FTP
curl: Multi-protocol file transfer command-line tool
上个截图,以下载zlib-devel为例:
其上几步走完才算把环境搭建完成,可是现在还没有到安装Node,还需要在Cywgin的ASH模式下执行rebaseall,步骤如下:
a. cmd命令行
b. 进入cygwin安装目录下的bin子目录
c. 运行ash进入shell模式
d. ./rebaseall -v
e. 没有错误就关闭命令行窗口
好了,现在到下载安装Node了,启动Cygwin.exe后输入:
$ wget http://nodejs.org/dist/node-v0.4.12.tar.gz
$ tar xf node-v0.4.12.tar.gz
$ cd node-v0.4.12
$ ./configure
$ make
$ make install
3.直接下载node.exe文件
nodejs.org下载较慢所以我在网盘上传了一个,下载地址:http://www.everbox.com/f/VhyL6EiGF5Lm3ZSRx85caFDIA5
听说有不太稳定的问题,不过你假如只是想先在windows下了解Node,个人感觉这个方法比你装个Cygwin好很多。
注:原本不太想写安装Node这段,可是为了这篇文章的全面性还是写了,没想到一写就是那么长一段了...茶几了
“Hello World” - 为什么每次见到这句心情都会小激动,不解...
首先,创建个hello.js的文件,在文件中copy如下代码:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, "127.0.0.1"); console.log('Server running at http://127.0.0.1:1337/');
代码逻辑:
a. 全局方法require()是用来导入模块的,一般直接把 require() 方法的返回值赋值给一个变量,在 JavaScript 代码中直接使用此变量即可 。require("http") 就是加载系统预置的 http 模块
b. http.createServer 是模块的方法,目的就是创建并返回一个新的web server对象,并且给服务绑定一个回调,用以处理请求。
c. 通过 http.listen() 方法就可以让该 HTTP 服务器在特定端口监听。
d. console.log就不用多说了,了解firebug的都应该知道,Node实现了这个方法。
注: 想了解具体细节请查看文档 cnodejs.org/cman/all.html#http.createServer
接着运行Node服务器,执行hello.js代码,成功启动会看见console.log()中的文本。有图有真相:
npm的下载和使用
除Node本身提供的API外,现在有不少第三方模块可极大的提高开发效率,npm就是Node的软件包管理器,可以用它安装所需软件包并发布自己为nodejs写的软件包。官网地址:npmjs.org
安装只需要在终端写入一行代码:
npm安装node扩展包同样是一行代码:
//例:npm install express
注:如果安装模块的过程中报域名错误的话,请清空缓存 >npm cache clean 或重启计算机即可。
理解Node的模块概念
在Node中,不同的功能组件被划分成不同的模块。应用可以根据自己的需要来选择使用合适的模块。每个模块都会暴露一些公共的方法或属性。模块的使用者直接使用这些方法或属性即可,对于内部的实现细节就可以不用了解。除了Node本身提供的API外,开发人员也可以利用这个机制来将应用拆分成多个模块,以提高代码的可复用性。
1.如何使用模块?
在Node中使用模块是非常方便的,在 JavaScript 代码中可以直接使用全局函数 require() 来加载一个模块。
在刚刚”Hello World"的例子中,require("http") 可以加载系统预置的 http 模块;模块名称以 "./" 开始的,如 require("./myModule.js") 用来加载与当前 JavaScript 文件同一目录下的 myModule.js 模块。
2.自己如何开发模块?
刚刚介绍使用require()导入模块的时候,模块名称以 "./" 开始的这种,就是自己开发的模块文件。需要注意的就是JS文件的系统路径。
代码中封装了模块的内部处理逻辑,一个模块一般都会暴露一些公开的方法或属性给其他的人使用。模块的内部代码需要把这些方法或属性给暴露出来。
3.来一套简单的例子。先创建一个模块文件如myModule.js,就一行代码
console.log('Hi Darren.')
然后创建一个test.js文件,导入这个JS文件,执行node看到结果
现在Node社区中已有不少第三方的模块,希望能有更多人通过学习Node,加入到这个大家庭中,为Node社区来添砖加瓦。先谢谢之,咱们继续。
4.来一个深点的例子。这个例子中将会针对 私有和共有 进行介绍。先创建一个myModule.js,代码如下:
var name = "Darren"; this.location = "Beijing"; this.showLog = function(){ console.log('Hi Darren.') };
代码中出现了三种类型,分别是: 私用属性,共有属性和共有方法,再创建一个test.js,执行Node
结果高亮的地方很清楚的告诉我们,私有方法我们在模块以外是取不到的,所以是undefined。共有属性和共有方法的声明需要在前面加上 this 关键字。
Node能做什么和它的优势
Node核心思想: 1.非阻塞; 2.单线程; 3.事件驱动。
在目前的web应用中,客户端和服务器端之间有些交互可以认为是基于事件的,那么AJAX就是页面及时响应的关键。每次发送一个请求时(不管请求的数据多么小),都会在网络里走一个来回。服务器必须针对这个请求作出响应,通常是开辟一个新的进程。那么越多用户访问这个页面,所发起的请求个数就会越来越多,就会出现内存溢出、逻辑交错带来的冲突、网络瘫痪、系统崩溃这些问题。
Node的目标是提供一种构建可伸缩的网络应用的方案,在hello world例子中,服务器可以同时处理很多客户端连接。
Node和操作系统有一种约定,如果创建了新的链接,操作系统就将通知Node,然后进入休眠。如果有人创建了新的链接,那么它(Node)执行一个回调,每一个链接只占用了非常小的(内存)堆栈开销。
举一个简单的异步调用的例子,把test.js和myMydule.js准备好了,^_^。把以下代码拷贝到test.js中并执行:
var fs = require('fs'); fs.readFile('./myModule.js', function (err, data) { if (err) throw err; console.log('successfully'); }); console.log('async');
所谓的异步,大家应该都能想得到运行时会先打先显示"async",再显示"successfully"。
Node是无阻塞的,新请求到达服务器时,不需要为这个请求单独作什么事情。Node仅仅是在那里等待请求的发生,有请求就处理请求。
Node更擅长处理体积小的请求以及基于事件的I/O。
Node不仅仅是做一个Web服务的框架,它可以做更多,比如它可以做Socket服务,可以做比方说基于文件的,然后基于像一些比方说可以有子进程,然后内部的,它是一个很完整的事件机制,包括一些异步非注射的解决方案,而不仅仅局限在网络一层。同时它可能,即使作为一个Web服务来说,它也提供了更多可以深入这个服务内核、核心的一些功能,比方说Node使用的Http Agent,这块就是它可以更深入这个服务内核来去做一些功能。
Node事件流概念
因为Node 采用的是事件驱动的模式,其中的很多模块都会产生各种不同的事件,可由模块来添加事件处理方法,所有能够产生事件的对象都是事件模块中的 EventEmitter 类的实例。代码是全世界通用的语言,所以我们还是用代码说话:
var events = require("events"); var emitter = new events.EventEmitter(); emitter.on("myEvent", function(msg) { console.log(msg); }); emitter.emit("myEvent", "Hello World.");
简单的分析这段:
1. 使用require()方法添加了events模块并把返回值赋给了一个变量
2. new events.EventEmitter()这句创建了一个事件触发器,也就是所谓的事件模块中的 EventEmitter 类的实例
3. on(event, listener)用来为某个事件 event 添加事件处理方法监听器
4. emit(event, [arg1], [arg2], [...]) 方法用来产生事件。以提供的参数作为监听器函数的参数,顺序执行监听器列表中的每个监听器函数。
EventEmitter 类中的方法都与事件的产生和处理相关:
1. addListener(event, listener) 和 on(event, listener) 这两个方法都是将一个监听器添加到指定事件的监听器数组的末尾
2. once(event, listener) 这个方法为事件为添加一次性的监听器。该监听器在事件第一次触发时执行,过后将被移除
3. removeListener(event, listener) 该方法用来将监听器从指定事件的监听器数组中移除出去
4. emit(event, [arg1], [arg2], [...]) 刚刚提到过了。
在Node中,存在各式各样不同的数据流,Stream(流)是一个由不同对象实现的抽象接口。例如请求HTTP服务器的request是一个流,类似于stdout(标准输出);包括文件系统、HTTP 请求和响应、以及 TCP/UDP 连接等。流可以是可读的,可写的,或者既可读又可写。所有流都是EventEmitter的实例,因此可以产生各种不同的事件。
可读流主要会产生以下事件:
data 当读取到流中的数据时,此事件被触发
end 当流中没有数据可读时,此事件被触发
error 当读取数据出现错误时,此事件被触发
close 当流被关闭时,,此事件被触发,可是并不是所有流都会触发这个事件。(例如,一个连接进入的HTTP request流就不会触发'close'事件。)
还有一种比较特殊的 fd 事件,当在流中接收到一个文件描述符时触发此事件。只有UNIX流支持这个功能,其他类型的流均不会触发此事件。
相关详细文档:http://cnodejs.org/cman/all.html#events_
强大的File System 文件系统模块
Node 中的 fs 模块用来对本地文件系统进行操作。文件的I/O是由标准POSIX函数封装而成。需要使用require('fs')访问这个模块。所有的方法都提供了异步和同步两种方式。
fs 模块中提供的方法可以用来执行基本的文件操作,包括读、写、重命名、创建和删除目录以及获取文件元数据等。每个操作文件的方法都有同步和异步两个版本。
异步操作的版本都会使用一个回调方法作为最后一个参数。当操作完成的时候,该回调方法会被调用。而回调方法的第一个参数总是保留为操作时可能出现的异常。如果操作正确成功,则第一个参数的值是 null 或 undefined 。
同步操作的版本的方法名称则是在对应的异步方法之后加上一个 Sync 作为后缀。比如异步的 rename() 方法的同步版本是 renameSync() 。下面列出来了 fs 模块中的一些常用方法,都只介绍异步操作的版本。
test.js和myModule.js文件准备好了木?把下面这段代码copy到test.js中执行一次
var fs = require('fs'); fs.unlink('./myModule.js', function (err) { if (err) throw err; console.log('successfully deleted myModule.js'); });
If no error is reported, then myModule.js will be deleted, it’s that simple
This is just a simple example. If you are interested, try it yourself and practice the truth. Due to space reasons, there are not many examples. ^_^
Summary of learning Node:
1. For me, who has almost zero knowledge of Linux commands and shells, I have learned a lot about Linux during this period; vim is really a powerful editor, and it feels really good not to use a mouse; And one thing that is very important to me is that programming under Linux is very cool, especially in the team where Windows is used, so it is healthier to install it^_^.
2. Understand Node, a successful framework for server-side JavaScript, as well as some of its advantages and usage methods. This article is the best summary. Of course, this is only the beginning.
3. I’m actually a little regretful that I didn’t get into such a dream company, but life should be like this, with ups and downs. This is what I need and expect... Then the new life must continue, Be your own helmsman, grasp your own direction, and let the past pass.
Something I want to say to everyone:
1. I have to discourage some people here. If you don’t know enough about background technology or have not been exposed to server-side languages, don’t know I/O knowledge, and don’t have the concept of background processing processes, then...Node is not a server-side technology suitable for entry. Why do you say this:
a. The key point is that there are few examples and articles in Chinese. It would be troublesome to learn the system systematically, so there is always a feeling of immaturity during use. Of course, it is mainly because I am not familiar with it. There are indeed not many companies using Node in China. Of course, there are still many overseas. I took a picture from cnodejs.org:
b. For inexperienced friends, node is actually not easy to get started with. It can be seen from the simplest "Hello world" (it takes some effort to understand various operating environments and installation details). Do not use jQuery Libraries are for comparison, they deal with different things, and the cost of learning is also different - so it is not recommended as a server-side technology for beginners to get started with. If you want to learn a server-side language, PHP and Python are both good choices, because: there are many examples in the book Multiple frameworks, easy to get started, easy to understand and easy to set up...
c. The above are my personal well-intentioned suggestions. Due to my limited level, please give me your advice and I hope you will be merciful.
2. I won’t show off the writing standards and specific techniques of Node. I don’t write much Node code myself, but object-oriented programming ideas are applicable everywhere.
The above content is the preliminary experience of nodejs introduced by the editor to you. I hope you like it.