工作中用到的一些node模块的简单memo

工作中用到的一些插件,简单做个memo
body-parser
express的中间件
bodyParser用于解析客户端请求的body中的内容,内部使用JSON编码处理,url编码处理以及对于文件的上传处理.
https://github.com/expressjs/body-parser

gulp
gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器;她不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成;使用她,我们不仅可以很愉快的编写代码,而且大大提高我们的工作效率。
gulp是基于Nodejs的自动任务运行器, 她能自动化地完成 javascript/coffee/sass/less/html/image/css 等文件的的测试、检查、合并、压缩、格式化、浏览器自动刷新、部署文件生成,并监听文件在改动后重复指定的这些步骤。在实现上,她借鉴了Unix操作系统的管道(pipe)思想,前一级的输出,直接变成后一级的输入,使得在操作上非常简单。通过本文,我们将学习如何使用Gulp来改变开发流程,从而使开发更加快速高效。
gulp 和 grunt 非常类似,但相比于 grunt 的频繁 IO 操作,gulp 的流操作,能更快地更便捷地完成构建工作。
gulp.task(name, fn)这个你应经见过了
gulp.run(tasks...)尽可能多的并行运行多个task
gulp.watch(glob, fn)当glob内容发生改变时,执行fn
gulp.src(glob)返回一个可读的stream
gulp.dest(glob)返回一个可写的stream
https://github.com/gulpjs/gulp

// 载入外挂
var gulp = require('gulp'),
browserify = require('browserify'),//这里用不上,管理js依赖的
source = require('vinyl-source-stream'),//同样这里用不上,和上面那个一起的
uglify = require('gulp-uglify'),//使用gulp-uglify压缩javascript文件,减小文件大小。
clean = require('gulp-clean'),//清理文件
notify = require('gulp-notify'),//加控制台文字描述用的
buffer = require('vinyl-buffer'),
less = require('gulp-less'),//转换less用的
autoprefixer = require('gulp-autoprefixer'),//增加私有变量前缀
minifycss = require('gulp-minify-css'),//压缩
concat = require('gulp-concat'),//合并
fileinclude = require('gulp-file-include'),// include 文件用
template = require('gulp-template'),//替换变量以及动态html用
rename = require('gulp-rename'),//重命名
webserver = require('gulp-webserver'),//一个简单的server,用python的SimpleHttpServer会锁文件夹
imagemin = require('gulp-imagemin'),//图片压缩
gulpif = require('gulp-if'),//if判断,用来区别生产环境还是开发环境的
rev = require('gulp-rev'),//加MD5后缀
revReplace = require('gulp-rev-replace'),//替换引用的加了md5后缀的文件名,修改过,用来加cdn前缀
addsrc = require('gulp-add-src'),//pipeline中途添加文件夹,这里没有用到
del = require('del'),//也是个删除···
vinylPaths = require('vinyl-paths'),//操作pipe中文件路径的,加md5的时候用到了
runSequence = require('run-sequence');//控制task顺序 Runs a sequence of gulp tasks in the specified order

一个简单的例子
var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');

var condition = true; // TODO: add business logic

gulp.task('task', function() {
gulp.src('./src/*.js')
.pipe(gulpif(condition, uglify()))
.pipe(gulp.dest('./dist/'));
});


express
--------------------------------------------------------------
serve-favicon
Node.js middleware for serving a favicon.
Examples

Typically this middleware will come very early in your stack (maybe even first) to avoid processing any other middleware if we already know the request is for /favicon.ico.

express

var express = require('express')
var favicon = require('serve-favicon')
var path = require('path')

var app = express()
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))

// Add your routes here, etc.

app.listen(3000)

--------------------------------------------------------------

morgan
morgan是express默认的日志中间件,也可以脱离express,作为node.js的日志组件单独使用
var express = require('express');
var app = express();
var morgan = require('morgan');

app.use(morgan('short'));
app.use(function(req, res, next){
res.send('ok');
});

app.listen(3000);
ode basic.js运行程序,并在浏览器里访问 http://127.0.0.1:3000 ,打印日志如下

➜ 2016.12.11-advanced-morgan git:(master) ✗ node basic.js
::ffff:127.0.0.1 - GET / HTTP/1.1 304 - - 3.019 ms
::ffff:127.0.0.1 - GET /favicon.ico HTTP/1.1 200 2 - 0.984 ms

http://www.cnblogs.com/chyingp/p/node-learning-guide-express-morgan.html

--------------------------------------------------------------


express 的 模板引擎
jade 和 ejs

jade
index.jade
doctype
html
head
title hello
body
h1 hello world


接着在命令行执行 jade -P index.jade ,编译后的文件 index.html 内容如下:
<!DOCTYPE html>
<html>
<head>
<title>hello</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>


ejs
特性

<% %> 用于控制流

<%= %> 用于转义的输出

<%- %> 用于非转义的输出


--------------------------------------------------------------
handlebars
模板引擎
在模板中使用{{ }}或{{{ }}}的地方就是表示
{{ }} 会转译标签
{{{ }}} 则直接替换

const productTemplateContent = fs.readFileSync(path.join(__dirname, '../templates/product.xml.hbs'), 'utf-8');
const productTemplate = handlebars.compile(productTemplateContent);
let html = productTemplate({ product: product, productJson: (new Buffer(JSON.stringify(product))).toString('base64') });


--------------------
crypto
node自带模块.这个模块的主要功能是加密解密。
var md5 = crypto.createHash(‘md5’);
var sha1 = crypto.createHash('sha1');

--------------------
needle
Nimble, streamable HTTP client for Node.js. With proxy, iconv, cookie, deflate & multipart support.
轻量级的http client模块,集成了iconv-lite,跟request类似
var needle = require('needle');

needle.get('http://www.google.com', function(error, response) {
if (!error && response.statusCode == 200)
console.log(response.body);
});


Callbacks not floating your boat? Needle got your back.

var data = {
file: '/home/johnlennon/walrus.png',
content_type: 'image/png'
};

needle
.post('https://my.server.com/foo', data, { multipart: true })
.on('readable', function() { /* eat your chunks */ })
.on('done', function() {
console.log('Ready-o, friend-o.');
})


needle.post(url, data, requestOptions, function(err, resp, body) {
let data;
if (body) {
let bodyText = body.toString('utf-8');
try {
data = JSON.parse(bodyText);
} catch (e) {
data = bodyText;
}
}
mainCallBack(err, body);
});

https://github.com/tomas/needle

--------------------
superagent
superagent是nodejs里一个非常方便的客户端请求代理模块,当你想处理get,post,put,delete,head请求时,你就应该想起该用它了:)

superagent 是一个轻量的,渐进式的ajax api,可读性好,学习曲线低,内部依赖nodejs原生的请求api,适用于nodejs环境下.


一个简单的post请求,并设置请求头信息的例子

request
.post('/api/pet')
.send({ name: 'Manny', species: 'cat' })
.set('X-API-Key', 'foobar')
.set('Accept', 'application/json')
.end(function(res){
if (res.ok) {
alert('yay got ' + JSON.stringify(res.body));
} else {
alert('Oh no! error ' + res.text);
}
});
https://cnodejs.org/topic/5378720ed6e2d16149fa16bd


SuperAgent请求的默认方法为GET,所以你可可以简单地写如下代码:

request('/search', function(res){

});

--------------------
--------------------

原文地址:https://www.cnblogs.com/sdfczyx/p/6652342.html