工做中用到的一些插件,簡單作個memo
body-parser
express的中間件
bodyParser用於解析客戶端請求的body中的內容,內部使用JSON編碼處理,url編碼處理以及對於文件的上傳處理.
https://github.com/expressjs/body-parserjavascript
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/gulpcss
// 載入外掛
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 orderhtml
一個簡單的例子
var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');前端
var condition = true; // TODO: add business logicjava
gulp.task('task', function() {
gulp.src('./src/*.js')
.pipe(gulpif(condition, uglify()))
.pipe(gulp.dest('./dist/'));
});node
express
--------------------------------------------------------------
serve-favicon
Node.js middleware for serving a favicon.
Examplespython
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.git
expressgithub
var express = require('express')
var favicon = require('serve-favicon')
var path = require('path')web
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){
});
----------------------------------------