标签归档:node.js

node被抛弃,iojs取而代之

从node分离出来农民起义的iojs终于取得了革命胜利。现在node的版本一下从0.12.x飞升到了4.0.0。

Node.js ChangeLog
2015-09-08, Version 4.0.0 (Stable), @rvagg

Notable changes

This list of changes is relative to the last io.js v3.x branch release, v3.3.0. Please see the list of notable changes in the v3.x, v2.x and v1.x releases for a more complete list of changes from 0.12.x. Note, that some changes in the v3.x series as well as major breaking changes in this release constitute changes required for full convergence of the Node.js and io.js projects.

child_process: ChildProcess.prototype.send() and process.send() operate asynchronously across all platforms so an optional callback parameter has been introduced that will be invoked once the message has been sent, i.e. .send(message[, sendHandle][, callback]) (Ben Noordhuis) #2620.
node: Rename "io.js" code to "Node.js" (cjihrig) #2367.
node-gyp: This release bundles an updated version of node-gyp that works with all versions of Node.js and io.js including nightly and release candidate builds. From io.js v3 and Node.js v4 onward, it will only download a headers tarball when building addons rather than the entire source. (Rod Vagg) #2700
npm: Upgrade to version 2.14.2 from 2.13.3, includes a security update, see https://github.com/npm/npm/releases/tag/v2.14.2 for more details, (Kat Marchán) #2696.
timers: Improved timer performance from porting the 0.12 implementation, plus minor fixes (Jeremiah Senkpiel) #2540, (Julien Gilli) nodejs/node-v0.x-archive#8751 nodejs/node-v0.x-archive#8905
util: The util.is*() functions have been deprecated, beginning with deprecation warnings in the documentation for this release, users are encouraged to seek more robust alternatives in the npm registry, (Sakthipriyan Vairamani) #2447.
v8: Upgrade to version 4.5.103.30 from 4.4.63.30 (Ali Ijaz Sheikh) #2632.
Implement new TypedArray prototype methods: copyWithin(), every(), fill(), filter(), find(), findIndex(), forEach(), indexOf(), join(), lastIndexOf(), map(), reduce(), reduceRight(), reverse(), slice(), some(), sort(). See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray for further information.
Implement new TypedArray.from() and TypedArray.of() functions. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray for further information.
Implement arrow functions, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions for further information.
Full ChangeLog available at https://github.com/v8/v8-git-mirror/blob/4.5.103/ChangeLog

以上是搬过来的changeLog,同时在changeLog页面往下翻一番,全都是iojs之前的版本号,很明显node原本的项目是直接被iojs项目覆盖掉了,注意这一行

node: Rename "io.js" code to "Node.js" (cjihrig) #2367.

iojs重命名为nodejs,革命军变成了正规军,推翻了原本的帝国统治。

现在点开iojs.org官网里的changelog也是直接跳转到https://github.com/nodejs/node/blob/master/CHANGELOG.md,也就是node的changelog。

 

这对native mod开发者来说是个福音,不用再去考虑兼容iojs和node携带的两个版本差距悬殊的v8版本了。

Error: Cannot find module ‘npmlog’

手动安装node的时候一不注意就会出现这个错误,装好了之后使用命令`npm` 测试一下出来了这个错误:`Error: Cannot find module ‘npmlog’` 。

 

这一般是放在/bin里的链接不对导致的。

可能情况1

链接指向的不是`node_modules/npm/bin/npm-cli.js` ,初次手动安装难免会搞不清要链接哪个文件,把链接指向正确的位置即可。

情况2

使用的不是软链接,由于npm-cli.js中使用的是相对路径,要是你一不小心链了个硬链接,那么npm就会从/bin解析相对路径,所以就找不到npmlog模块了。要使用软链接,在ln命令后加上`-s` 选项:`ln -s node_modules/npm/bin/npm-cli.js /bin/npm`

 

解决node无法调用全局模块的问题

刚刚我把SPDY装到全局,`npm -g install spdy` ,

spdy@2.0.4 /usr/local/lib/node_modules/spdy
├── http-deceiver@1.2.4
├── handle-thing@1.2.4
├── select-hose@2.0.0
├── debug@2.2.0 (ms@0.7.1)
└── spdy-transport@1.1.8 (obuf@1.1.1, wbuf@1.7.0, hpack.js@2.1.4, readable-stream@2.0.2)

可以看到这个模块被装到/usr/local/lib/node_modules里了

 

 

 

然后开个环境测试一下,结果

> require('spdy')
Error: Cannot find module 'spdy'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:286:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at repl:1:1
    at REPLServer.defaultEval (repl.js:154:27)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:308:12)
    at emitOne (events.js:77:13)

吓得我满脑子都是doge!!!这是为什么?

继续阅读解决node无法调用全局模块的问题

[Node.js]用ws模块创建加密的ws服务(wss)

node的ws模块可以很方便地创建一个单纯的标准websocket服务,但是对于创建wss服务并没有提供一个独立的方法,还是使用创建ws服务的方法,在传入参数对象里加了个自定义的https服务。

 

也不知道是我找错了文档还是官方文档就那么简洁,不过readme文件写的文档的确是这个→doc/ws.md,然后我研究了好几天还翻了遍它的源码和examples才终于理解了这个文档。

继续阅读[Node.js]用ws模块创建加密的ws服务(wss)

[Node.js]批量重命名音乐专辑内音乐文件名

这只是个便签博,方便以后再拿出来用。。

刚刚下了个LL大法的全套音乐专辑,(原谅我又盗版了),然后里面每个CD的音乐都是一如既往有编号的,不过我们放进自己播放列表的话并不需要这些编号,所以我要把它们去掉。

 

作为一个程序员,怎么能一个个手动改名呢,这简直是耻辱!

继续阅读[Node.js]批量重命名音乐专辑内音乐文件名

[Node.js]isFile

昨天被nodejs的`isfile` 坑了一下午。

本来我是这么用的

var fs=require("fs");
var stat=new fs.Stats();

stat.isFile(文件路径);

结果没有任何返回,也没有报错。于是我就研究了一下午是不是相对路径问题,是不是系统问题什么的。。。

为什么我要这么写呢,因为API文档很简单就带过了这个内容,连个例子也没有:


 

Class: fs.Stats

`fs.stat()` , `fs.lstat()`  和 `fs.fstat()`  以及他们对应的同步版本返回的对象。

stats.isFile()
stats.isDirectory()
stats.isBlockDevice()
stats.isCharacterDevice()
stats.isSymbolicLink() (仅在与 fs.lstat()一起使用时合法)
stats.isFIFO()
stats.isSocket()

 


由于看到了`class fs.Stats` ,我第一反应就是先`new` 一个出来,结果就悲剧了。后来经过不断探索,昂头奋进,披荆斩棘,终于从废渣信息爆满的百度里找到了`isFile` 的正确用法,是这样:

var fs=require("fs");
fs.statSync(路径).isFile();//同步版

fs.stat(path,function(s){//异步版
	callback(s.isFile());
});

`isDirectory()` 和`isFile()` 同理

接触node.js有感

今天我出于好奇接触了一下node.js。
由于还有点js基础,看起来不是很费劲,先不说我学到了什么,我想说的是我发现了自己一直以来的错误观念。

原来我认为:访问一个url,它的地址就是对应的服务器路径。比如http:\\blog.luojia.me\1.html,这里1.html就是对应网站目录里的1.html。
但其实并不是,url应该当作一整个字符串来看而不是分成目录层级或文件,如何处理这个1.html文件的请求完全是由服务器程序说了算的。

也许和我有一样误解的人还没看懂是啥意思,那就举个例子:
我在用node.js启动一个最简单的hello world服务器时,访问了同目录下的其它文件,结果浏览器结果还是那个hello world页面,也就是服务器在不解析url的情况下,完全不可能访问到任何文件。这个问题自己尝试的时候比较好理解,但是就我这极渣的表达能力可能说不清楚。

总之就是说明了url路径和服务器路径并没有一定的相关关系,这也就是服务器路径重写的原理。

另外nodejs给了我一点惊奇,我原以为它类似于php需要个像apache或iis这样的服务器来运行,没想到它本身就可以做成服务端(虽然PHP似乎也有内置的服务器功能)。从编写代码层面来说,如果php是用钢筋混凝土在造房子,那nodejs就像在造钢筋混凝土材料,然后再搭房子。虽然当我看到连静态文件都得自己写代码路由的时候我有点想找小伙伴来一起惊呆,不过从零配件角度来看还是可以理解的。

再另外,每改一次代码我就得ctrl+c停掉服务器再启动真是太不科学的设计了吖,难道没有debug模式自动监视文件更改吗。(到底有没有我也不知道,有的话求方法)