`
since1027
  • 浏览: 7777 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论
文章列表

7.NodeJS大示例

7.NodeJS大示例 学习讲究的是学以致用和融会贯通。至此我们已经分别介绍了NodeJS的很多知识点,本章作为最后一章,将完整地介绍一个使用NodeJS开发Web服务器的示例。 需求 我们要开发的是一个简单的静态文件合并服务器,该服务器需要支持类似以下格式的JS或CSS文件合并请求。 http://assets.example.com/foo/??bar.js,baz.js   在以上URL中,??是一个分隔符,之前是需要合并的多个文件的URL的公共部分,之后是使用,分隔的差异部分。因此服务器处理这个URL时,返回的是以下两个文件按顺序合并后的内容。 /foo/bar.j ...

6.异步编程

6.异步编程 NodeJS最大的卖点——事件机制和异步IO,对开发者并不是透明的。开发者需要按异步方式编写代码才用得上这个卖点,而这一点也遭到了一些NodeJS反对者的抨击。但不管怎样,异步编程确实是NodeJS最大的特点,没有掌 ...

5.进程管理

5.进程管理 NodeJS可以感知和控制自身进程的运行环境和状态,也可以创建子进程并与其协同工作,这使得NodeJS可以把多个程序组合在一起共同完成某项工作,并在其中充当胶水和调度器的作用。本章除了介绍与之相关的NodeJS内置模块外,还会重点介绍典型的使用场景。 开门红 我们已经知道了NodeJS自带的fs模块比较基础,把一个目录里的所有文件和子目录都拷贝到另一个目录里需要写不少代码。另外我们也知道,终端下的cp命令比较好用,一条cp -r source/* target命令就能搞定目录拷贝。那我们首先看看如何使用NodeJS调用终端命令来简化目录拷贝,示例代码如下: var chi ...

4.网络操作

4.网络操作 不了解网络编程的程序员不是好前端,而NodeJS恰好提供了一扇了解网络编程的窗口。通过NodeJS,除了可以编写一些服务端程序来协助前端开发和测试外,还能够学习一些HTTP协议与Socket协议的相关知识,这些知识在优化前端性能和排查前端故障时说不定能派上用场。本章将介绍与之相关的NodeJS内置模块。 开门红 NodeJS本来的用途是编写高性能Web服务器。我们首先在这里重复一下官方文档里的例子,使用NodeJS内置的http模块简单实现一个HTTP服务器。 var http = require('http'); http.createServer(functio ...

3.文件操作

3.文件操作 让前端觉得如获神器的不是NodeJS能做网络编程,而是NodeJS能够操作文件。小至文件查找,大至代码编译,几乎没有一个前端工具不操作文件。换个角度讲,几乎也只需要一些数据处理逻辑,再加上一些文件操作,就能够编写出大多数前端工具。本章将介绍与之相关的NodeJS内置模块。 开门红 NodeJS提供了基本的文件操作API,但是像文件拷贝这种高级功能就没有提供,因此我们先拿文件拷贝程序练手。与copy命令类似,我们的程序需要能接受源文件路径与目标文件路径两个参数。 小文件拷贝 我们使用NodeJS内置的fs模块简单实现这个程序如下。 var fs = require('f ...
2.代码的组织和部署 有经验的C程序员在编写一个新程序时首先从make文件写起。同样的,使用NodeJS编写程序前,为了有个良好的开端,首先需要准备好代码的目录结构和部署方式,就如同修房子要先搭脚手架。本章将介绍与之相关的各种知识。 模块路径解析规则 我们已经知道,require函数支持斜杠(/)或盘符(C:)开头的绝对路径,也支持./开头的相对路径。但这两种路径在模块之间建立了强耦合关系,一旦某个模块文件的存放位置需要变更,使用该模块的其它模块的代码也需要跟着调整,变得牵一发动全身。因此,require函数支持第三种形式的路径,写法类似于foo/bar,并依次按照以下规则解析路径,直 ...
1.NodeJS基础 什么是NodeJS JS是脚本语言,脚本语言都需要一个解析器才能运行。对于写在HTML页面里的JS,浏览器充当了解析器的角色。而对于需要独立运行的JS,NodeJS就是一个解析器。 每一种解析器都是一个运行环境,不但允 ...
 虽然现在用Apache Commons DBCP可以非常方便的建立数据库连接池,但是像这篇文章把数据库连接池的内部原理写的这么透彻,注视这么完整,真是非常难得。让开发人员可以更深层次的理解数据库连接池,真是非常感谢这篇文章的作者。   import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; ...
In this article, we will look at events in Backbone.js. We will see how backbone provides us events and how we can use backbone events in our application. Background Events are a vital part of any application framework. Events are useful mainly in two scenarios when it comes to applications. Events ...
In this article, we will try to look at Routes in Backbone.js. We will try to understand how routes can be useful in a large scale single page applications and how we can use routes to perform action based on requested URL. Background We have been using web application for more than 2 decades now. ...
In this article, we will try to look at the View classes in Backbone.js and see how view classes help us in updating the relevant parts of the application easily. Background The biggest problem while writing JavaScript applications is the spaghetti code that one needs to write just for HTML DOM man ...
In this article we will discuss about Backbone.js collections. We will see how we can use collections to manipulate a group of models and how we can use restul API to easily fetch and save collections. Background Every application need to create a collection of models which can be ordered, iterated ...
In this article we will discuss how we can perform CRUD operations on a backbone model using a REST based HTTP service. Background Earlier we have discussed about the benefits of using backbone.js and we also looked at the backbone models. In this article we will look at performing the CRUD operat ...
In this article we will look at some more concepts related to backbone models. We will try to see how we can override the default model behavior. We will look at the signification of model IDs, how we can validate a model and finally how a model can be persisted either locally or on a server. Link t ...
When we talk about any MV* pattern, model is undoubtedly the most important part of the architecture/application. Its the model that contains all the application data. Along with keeping the data the model class performs various set of actions on the data. Actions like possibility to validate the dat ...
Global site tag (gtag.js) - Google Analytics