惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Stack Overflow Blog
Stack Overflow Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
WordPress大学
WordPress大学
B
Blog RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
MyScale Blog
MyScale Blog
GbyAI
GbyAI
Martin Fowler
Martin Fowler
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 聂微东
The Cloudflare Blog
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
I
InfoQ
Google DeepMind News
Google DeepMind News
S
Securelist
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 【当耐特】
Latest news
Latest news
T
Threatpost
量子位
Y
Y Combinator Blog
T
Troy Hunt's Blog
Know Your Adversary
Know Your Adversary
MongoDB | Blog
MongoDB | Blog
罗磊的独立博客
博客园_首页
AWS News Blog
AWS News Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
宝玉的分享
宝玉的分享
Project Zero
Project Zero
V
Visual Studio Blog
F
Fortinet All Blogs
S
Security Affairs
The Register - Security
The Register - Security
G
Google Developers Blog
T
Tenable Blog
L
LINUX DO - 最新话题
The GitHub Blog
The GitHub Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
博客园 - Franky

博客园 - kylindai

Spark installation for windows java cpu load - kylindai remove all .git files and directories use one command - kylindai Android adb not responsing putty ssh login linux nodejs 实现 http proxy 透明转发 mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler 一次PostgreSql数据迁移,使用nodejs来完成 nodejs 安装 postgresql module mongodb 安装为windows服务 memcached linux / win32 1.4.13 learning nodejs 1 - stream.pipe javascript的变量、作用域和内存问题 javascript编程的最佳实践推荐 android download host jetty-distribution-7.6.x 部署 Quartz Cron 表达式 ImageMagick 批量处理图片脚本 常用 LINUX 命令
learning nodejs 2 - connect middleware
kylindai · 2014-02-01 · via 博客园 - kylindai

学习了connect module

nodejs 的中间件方式

var connect = require('connect');
var server = connect.createServer();

// connect.logger 是一个中间件 server.use(connect.logger(
'dev'));
// 值得学习的是 next 方法 server.use(
function(req, res, next) { if ('/a' == req.url) { // here do something } else { // next is the require chain like java filter china next(); } });

server.listen(3000);

connect 的中间件,还包括:

connect.static // 映射静态路径 

// web root path
server.use(connect.static(__dirname + '/resource'));

// path '/images' point to '/path/to/images'
server.use('/images', connect.static('/path/to/images'));

// client side cache
server.use('/js', connect.static('/path/to/bounds', {
    maxAge: 10000000 });

// hidden
server.use(connect.static('/path/to/resources', { 
    hidden: true });

connect.query // 解析GET参数

connect.bodyParser // 解析POST参数

connect.logger // 日志

connect.cookieParser // 解析cookie

connect.session // session

var server = connect(
  connect.logger('dev'),
  connect.bodyParser(),
  connect.cookieParser(),
  connect.session({secret: 'my app secret'}),
  function(req, res, next) {
     if (req.url == '/images') {
        // todo ... 
     } else {
        next();
     }
  }
);

nodejs的代码真是简洁啊~