
Node.JS
diandian82
这个作者很懒,什么都没留下…
展开
-
Adding timeout support for http request in Node.JS
This is HttpUtil Module, you need "require" it before using the module. var http= require("http"); var HttpUtil = function(){ var namespace = { Get: function(options, timeout, callback){ v原创 2013-05-18 13:26:57 · 1158 阅读 · 0 评论 -
Working with promise and generators
var Q = require('q'); function asyncSquare(n) { var defer = Q.defer(); setTimeout(function(){ if(n>5) defer.reject(888) defer.resolve(n*n); },2000) return defer.promise; }原创 2014-08-23 07:32:05 · 561 阅读 · 0 评论 -
Using HTTP Proxy in Node.js with request package
const request = require('request'); request({'url':'http://example.com', 'proxy':'http://127.0.0.1:8888'}, function (error, response, body) { if (!error && response.statusCode == 200) {原创 2014-08-09 02:16:07 · 1009 阅读 · 0 评论 -
通过Wiston创建log,一天一文件
/** * User access, app run and error log. * * @module Logger */ //import var fs = require('fs'); var util = require('util'); var winston = require('winston'); var mkdirp = require('mkdirp'); va原创 2014-03-24 11:18:24 · 2423 阅读 · 0 评论 -
Javascript format numbers
Round to a certain number of places For rounding decimals you can use the built-in JavaScript methods toFixed or toPrecision. var num = 10; var result = num.toFixed(2); // result will equal 10.00 n转载 2013-07-10 13:13:41 · 642 阅读 · 0 评论 -
use nodemailer to send gmail
1. sudo npm install nodemailer 2. Import the following class to your source code var nodemailer = require("nodemailer"); function MailPoster( to, subject, body){ this.from = 'XXXXXXX@gmail.com';原创 2013-06-07 14:07:11 · 1721 阅读 · 0 评论 -
js正则表达式笔记
1. 匹配整个单词列表 var reg = /\b(shoes|shirt|pants)\b/;原创 2013-06-06 15:25:04 · 824 阅读 · 0 评论 -
示例:js使用正则表达式group来提取字符串中的数据
var linkStr = "/black-mountain/35-cotton-creek-cir-black-mountain-nc-421_537763.html"; // 括号表示组。访问可以用group[index]来访问每组的信息 var linkRegx = /\/([^\/]+)\/.+-(\d+)_(\d+).html/; var group = linkStr.match(li原创 2013-06-04 16:58:28 · 24039 阅读 · 0 评论 -
Hogan.js 使用pratial示例
var hogan = require("hogan.js"); var template = hogan.compile('{{#list}}{{foo}}{{> par}}\n{{/list}}'); var partial = hogan.compile('/*{{partialData.name}}-{{partialData.age}}*/'); var result = templa原创 2013-05-24 15:40:48 · 3391 阅读 · 0 评论 -
构造函数+原型混合方式实现js的继承
function ClassA(sColor){ this.color = sColor; } ClassA.prototype.sayColor = function(){ console.log(this.color); } function ClassB(sColor, sName){ ClassA.call(this, sColor); this.Name = sName;原创 2013-05-26 19:30:36 · 1703 阅读 · 0 评论 -
Use command line arguments in Node.js
var args = process.argv.splice(2); if(!args || args.length ==0){ console.log("no params"); } for(var i=0;i<args.length;i++) console.log(args[i]); commandline: node test1.js a b c d output: a原创 2013-05-23 10:45:12 · 1573 阅读 · 0 评论 -
自制ES6 Promise
Checked all Promise sample, none of them is in ES6 syntax, this is weird, which age are we now. Simplest verison class MyPromise { constructor(fn) { this.doneCallback = null; fn(t原创 2017-12-23 01:51:26 · 227 阅读 · 0 评论