http的get示例
return new Promise(function(resolve, reject) {
http.get("http://localhost', function(res) {
var bufs = [];
res.on('data', bufs.push.bind(bufs))
.on('end', function() {
var str = Buffer.concat(bufs).toString();
if(str && str[0] === '{') {
var obj = JSON.parse(str);
return resolve(obj.ret);
} else {
return resolve(false);
}
});
});
});
URL字符的码集
答案 [a-zA-Z0-9] $-_.+!*'(),%
"...Only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'(),"
[not including the quotes - ed], and reserved characters used for their
reserved purposes may be used unencoded within a URL."
“只有字母和数字[0-9a-zA-Z]、一些特殊符号“$-_.+!*'(),”
[不包括双引号]、以及某些保留字,才可以不经过编码直接用于URL。”
将随机数编成定长的16进制
Math.floor(Math.random()*0xfffff | 0x100000).toString(16).substr(1)
string.substr(start, length)
string.substring(from, to)
转换十进制成16进制
How to convert decimal to hex in JavaScript?
hexString = yourNumber.toString(16);
yourNumber = parseInt(hexString, 16);
'.'.charCodeAt(0).toString(16)
promise的多个finally和timer间有竞争条件吗
什么意思?
nodejs child_process exit事件和close事件的区别
This event is emitted when the stdio streams of a child process have all terminated. This is distinct from 'exit', since multiple processes might share the same stdio streams.
Spawn时,stdio选项可以是ipc或者stream object,它们是共享的文件描述符。
如何把 %E4%B8%AD 转成汉字
decodeURIComponent('%E4%B8%AD')
创建一个自签名的https服务器
https://cnodejs.org/topic/54745ac22804a0997d38b32d
koa教程
http://koa.rednode.cn/
把string转化成一个stream
var s = new stream.Readable();
s._read = function noop() {}; // redundant? see update below
s.push('your text here');
s.push(null);
把stream转化成一个string,
The key is to use these two Stream events:
Event: 'data'
Event: 'end'
For stream.on('data', ...) you should collect your data data into either a Buffer (if it is binary) or into a string.
For on('end', ...) you should call a callback with you completed buffer, or if you can inline it and use return using a Promises library.
new Function多个参数
http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
var f = new (Function.prototype.bind.apply(Function, [null, 'a', 'b', 'return a*b']));
运行new Cls()时,参数数目是固定的。但是bind方法可以:
var f = Cls.bind(anything, arg1, arg2, ...);
result = new f();
anything是什么没关系,因为new操作重置了f的上下文。只是句法的需要。
现在,对于bind调用,我们要传入可变参数,那就这样:
var f = Cls.bind.apply(Cls, [anything, arg1, arg2, ...]);
result = new f();
Cls.bind是个bind函数,但是Cls.bind可能被覆盖。所以用Function.prototype.bind替换。
eval or new Function
http://dfkaye.github.io/2014/03/14/javascript-eval-and-function-constructor/
eval执行代码是,作用域设在当前执行作用域,可以访问局部变量。
The eval function has access to the global scope, so it can clobber any globals as well. Function shares this problem.
eval可以访问全局作用域,所以它能狠揍(哈哈)任何全局变量。Function不存在这个问题。
new Function不能访问当前作用域。可以访问全局。
new Function是都会修改全局的。狠揍的意思是定义变量不写var前缀。
eval是,如果当前中有,则修改当前的。如果没有则狠揍全局的。