node.js_in_practice(intermediate)

Node.js in practice(intermediate)

1. Node Foundations

1.1 Features of node.js

  • 非阻塞I/O none-blocking IO
  • 利于爬虫
  • 与JSON 配合的很好

nodejs 主要特性

  • node.js 的标准库由两部分构成

这里写图片描述
binary libraries 与 core modules

binary libraries 为二进制文件 core modules 是用js 编写

1.2 建立一个新程序

  1. 创建文件夹 nodeap
  2. 创建一个app.js 文件 (如果不创建, npm init 后 main 入口会为index.js)
  3. 在app.js 文件中写代码
  4. npm init ,会自动生成package.json 文件(入口为 app.js)

2.Node’s global objects and methods

2.1 Modules

  1. Installing and loading modules

    • 如果想查找modules: npm search express (正则表达式: npm search /^express$/ )
    • 本地安装 npm install module-name
    • 全局安装(nodemon)npm install -g mudule-name
  2. Creating and managing modules

    • 使用require ,返回一个对象(object)
    • 一旦一个module 被required了,它将被缓存,这意味着你多次require 返回的将是缓存下来的模块
    • delete require.cache[require.resolve(‘./myclass’)]; 删除被缓存的module
  3. Loading a group of related modules

    • 文件夹中创建一个index.js 的文件,在index.js文件中组织其他文件
    • 或者创建一个package.json 文件,指明main属性

    案例如下:

    这里写图片描述
    index.js 文件中的代码如下

    module.exports ={
        one: require('./one'),
        two: require('./two')
     }

    在app.js 中

      var mymodule = require('./mymodule');
       mymodule.one();
       mymodule.two(); 

    或者在mymodule 文件夹中添加一个package.json 文件:(测试无效)

  4. Working with paths

    • 使用__dirname 和__filename 来定位文件
      指的是当前文件所在的文件夹路径名

    可以使用

    varview=__dirname+'/views/view.html'

    或者使用path模块

     path =require('path')
     path.join(__dirname, 'views', 'view.html');.

2.2 Standard I/O and the console object

  1. Reading and writing to standard I/O

    process.stdin.resume();
    process.stdin.setEncoding('utf8');
    process.stdin.on('data',function(text){
    process.stdout.write(text.toUpperCase());
              });
    

视图如下:
![image](/Users/lishuang/Desktop/nodeprocess.tiff
)

  1. Benchmarking a program

使用console.time(‘label’)
console.timeEnd(‘label’)

2.3 Operating system and command-line integration

  1. Getting platform information

    • process 一般是用来处理与操作系统相关的信息

    • 使用 process.arch 与 process.platform 属性来处理相关信息
      process.arch 查看当前处理器平台

    ‘arm’, ‘ia32’, or ‘x64’.

    • process.memoryUsage() 内存使用情况
  2. Passing command-line arguments

process.argv

2.4 Delaying execution with timers

1.setTimeout 与 Function.prototype.bind()

3. Buffers

Buffer 是全局类型,可以用来处理二进制数据

var fs = require('fs');
var greet = fs.readFileSync(__dirname+'/greet.txt','utf8');
console.log(greet);

var greet2 = fs.readFile(__dirname+'/greet.txt',function(err,data){
    console.log(data.toString());
});
console.log('Done');

fs.readFile(__dirname+'/greet.txt',function(err,buf){
    if(err) console.log("read file error");
    console.log(buf.toString());
    console.log(Buffer.isBuffer(buf));  
    console.log(Buffer.byteLength(buf.toString(),'utf8'));
})

Buffer.isBuffer(obj); //判断对象是否为buffer
Buffer.byteLength(str,’utf8’) ,区别于str.length(一个字符可能占用多个字节)

给出一个写图片的例子

     var mine = 'test1.png';
     var encoding = 'base64';

     var data = fs.readFileSync(__dirname+"/test1.png").toString(encoding); //字符串

     var buf = new Buffer(data,encoding);//创建buffer 

      fs.writeFileSync(__dirname+'/copy.png',buf);

4.Events

关于EventEmitter模块

 var util = require('util');
var EventEmitter = require('events');

function AppleMusic(){
    EventEmitter.call(this);  // 继承了EventEmitter的ownProperty
    this.name = "You are my sunshine";
    this.playing =false;
}
util.inherits(AppleMusic,EventEmitter); //  继承了 EventEmitter 的原型

var music = new AppleMusic();

music.on('play',function(){
    this.playing = true;
})

music.on('play',function(track){
    this.playing = true;
    console.log("The track is: "+ track);
})
music.on('stop',function(){
    this.playing = false;
    console.log("Stoped the music")
})

music.emit('play',"你发如雪,就没了离别,我爱你如三千东流水") ;
setTimeout(function(){
    music.emit('stop');
},5000);

5.Streams

在上文中我们知道
fs.readFile() 于fs.readFileSync() 都是将数据全部读到内存中去,也就是Buffer中,

如果有一种方法,能够读取一部分数据(chunk),然后处理它(process it),然后继续读进来更多的数据

以下链接提供了一个如何在node.js 中使用streams 的指南
https://github.com/substack/stream-handbook/

图片如下:

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
英文原版,无水印,数字版,有目录。 Node.js in Practice 1st Edition Summary Node.js in Practice is a collection of fully tested examples that offer solutions to the common and not-so-common issues you face when you roll out Node. You'll dig into important topics like the ins and outs of event-based programming, how and why to use closures, how to structure applications to take advantage of end-to-end JavaScript apps, and more. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Book You've decided to use Node.js for your next project and you need the skills to implement Node in production. It would be great to have Node experts Alex Young and Marc Harter at your side to help you tackle those day-to-day challenges. With this book, you can! Node.js in Practice is a collection of 115 thoroughly tested examples and instantly useful techniques guaranteed to make any Node application go more smoothly. Following a common-sense Problem/Solution format, these experience-fueled techniques cover important topics like event-based programming, streams, integrating external applications, and deployment. The abundantly annotated code makes the examples easy to follow, and techniques are organized into logical clusters, so it's a snap to find what you're looking for. Written for readers who have a practical knowledge of JavaScript and the basics of Node.js. What's Inside Common usage examples, from basic to advanced Designing and writing modules Testing and debugging Node apps Integrating Node into existing systems About the Authors Alex Young is a seasoned JavaScript developer who blogs regularly at DailyJS. Marc Harter works daily on large-scale projects including high-availability real-time applications, streaming interfaces, and other data-intensive systems. Table of Contents PART 1 NODE FUNDAMENTALS Getting started Globals: Node's environment Buffers: Working with bits, bytes, and encodings Events: Mastering EventEmitter and beyond Streams: Node's most powerful and misunderstood feature File system: Synchronous and asynchronous approaches Networking: Node's true "Hello, World" Child processes: Integrating external applications with Node PART 2 REAL-WORLD RECIPES The Web: Build leaner and meaner web applications Tests: The key to confident code Debugging: Designing for introspection and resolving issues Node in production: Deploying applications safely PART 3 WRITING MODULES Writing modules: Mastering what Node is all about
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值