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

推荐订阅源

Google DeepMind News
Google DeepMind News
I
InfoQ
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
GbyAI
GbyAI
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
美团技术团队
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
M
MIT News - Artificial intelligence
D
Docker
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 叶小钗

博客园 - 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的代码真是简洁啊~