《Node.js In Action》系列--Node programming fundamentals

1 内容提要

  • 使用module组织代码
  • 编码规范
  • 使用callback处理一次性事件
  • 使用event emitter处理重复事件
  • 实现串行流和并行流的控制


2 总结


2.1 使用module组织代码

假设我们现在面临一个自己编写的代码与第三方库发生命名冲突的问题,有两种方法:一,改变自己的,二,改变别人的。如果改变自己的,就会影响到所有使用到这段代码的地方,而如果改变别人的,那么每次更新第三方库就要改一遍名字,这两种方法的影响都不小,所以最好完全避免命名冲突。

Node的module就很好地解决了命名冲突的问题。通过exports对象或者module.exports,我们可以选择application所需的方法或变量。

2.1.1 创建module。

module可以是一个文件,也可以是文件夹。如果module是一个文件夹,一般而言,入口文件将以index.js命名。下面以一个简单的货币转换module为例。

首先创建一个名为currency_app的文件夹,在这个文件夹下创建一个currency.js,它就是一个module,内容如下:

var canadianDollar = 0/91;

function roundTwoDecimals(amount) {
  return Math.round(amount * 100) / 100;
}
exports.canadianToUS = function(canadian) {
  return roundTwoDecimals(canadian * canadianDallar);
}
exports.USToCanadian = function(us) {
  return roundTwoDeciamls(us / canadianDollar);
}

只为exports对象设置了两个属性,一个是canadianToUS,一个是USToCanadian,也就是说,currency module所在的应用只能使用canadianToUS和USToCanadian两个属性。而canadianDollar就变为了一个私有变量,仅供module中的其他方法使用。

为了用这个currency module,还需要在currency_app下加一个test-currency.js,其内容如下:

var currency = require('./currency');

console.log('50 Canadian dollars equals this amount of US dollars: ');
console.log(currency.canadianToUs(50));

console.log('30 US dollars equals this amount of canadian dollars: ');
console.log(currency.USToCanadian(30));

这两个文件的结构如图:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The first edition of Node.js in Action was about web development with a particular focus on the Connect and Express web frameworks. Node.js in Action, Second Edition has been updated to suit the changing requirements of Node development. You’ll learn about front-end build systems, popular Node web frameworks, and how to build a web application with Express from scratch. You’ll also learn how to create automated tests and deploy Node web applications. Node is being increasingly used for command-line developer tools and desktop applications with Electron, so you’ll find chapters dedicated to both of these areas. This book assumes you’re familiar with basic programming concepts. The first chapter provides an overview of JavaScript and ES2015 for those of you who haven’t yet discovered the joys of modern JavaScript. Roadmap This book is organized into three parts. Part 1 provides an introduction to Node.js, teaching the fundamental techniques needed to develop with it. Chapter 1 explains the characteristics of JavaScript and Node and steps through example code. Chapter 2 guides you through fundamental Node.js programming concepts. Chapter 3 is a full tutorial on how to build a web application from scratch. Part 2, the largest section of the book, focuses on web application development. Chapter 4 dispels some of the mystery around front-end build systems: if you’ve ever had to use webpack or Gulp in a project but didn’t really understand it, this is the chapter for you. Chapter 5 reviews some of the most popular server-side frameworks available for Node, and chapter 6 goes into Connect and Express in more depth. Chapter 7 is dedicated to templating languages, which can improve your productivity when writing server-side code. Most web applications need a database, so chapter 8 covers the many types of databases that you can use with Node, from relational to NoSQL. Chapters 9 and 10 deal with testing and deployment, and this includes cloud deployment. Part 3 goes be

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值