Connect is a middleware layer for Node.js

Connect is a middleware layer for Node.js   http://senchalabs.github.com/connect

Connect

Connect is an extensible HTTP server framework for node using "plugins" known as middleware.


var connect = require(‘connect‘)
var http = require(‘http‘)
var app = connect()
// gzip/deflate outgoing responses
var compression = require(‘compression‘)
app.use(compression())
// store session state in browser cookie
var cookieSession = require(‘cookie-session‘)
app.use(cookieSession({
    keys: [‘secret1‘, ‘secret2‘]
}))
// parse urlencoded request bodies into req.body
var bodyParser = require(‘body-parser‘)
app.use(bodyParser.urlencoded())
// respond to all requests
app.use(function(req, res){
  res.end(‘Hello from Connect!\n‘);
})
//create node.js http server and listen on port
http.createServer(app).listen(3000)
Getting Started

Connect is a simple framework to glue together various "middleware" to handle requests.

Install Connect

$ npm install connect

Create an app

The main component is a Connect "app". This will store all the middleware added and is, itself, a function.

var app = connect();

Use middleware

The core of Connect is "using" middleware. Middleware are added as a "stack" where incoming requests will execute each middleware one-by-one until a middleware does not call next() within it.

app.use(function middleware1(req, res, next) { // middleware 1 next(); }); app.use(function middleware2(req, res, next) { // middleware 2 next(); });

Mount middleware

The .use() method also takes an optional path string that is matched against the beginning of the incoming request URL. This allows for basic routing.

app.use(‘/foo‘, function fooMiddleware(req, res, next) { // req.url starts with "/foo" next(); }); app.use(‘/bar‘, function barMiddleware(req, res, next) { // req.url starts with "/bar" next(); });

Error middleware

There are special cases of "error-handling" middleware. There are middleware where the function takes exactly 4 arguments. Errors that occur in the middleware added before the error middleware will invoke this middleware when errors occur.

app.use(function onerror(err, req, res, next) { // an error occurred! });

Create a server from the app

The last step is to actually use the Connect app in a server. The .listen() method is a convenience to start a HTTP server.

var server = app.listen(port);

The app itself is really just a function with three arguments, so it can also be handed to .createServer() in Node.js.

var server = http.createServer(app);

Middleware

These middleware and libraries are officially supported by the Connect/Express team:

Most of these are exact ports of their Connect 2.x equivalents. The primary exception is cookie-session.

Some middleware previously included with Connect are no longer supported by the Connect/Express team, are replaced by an alternative module, or should be superseded by a better module. Use one of these alternatives instead:

Checkout http-framework for many other compatible middleware!

Running Tests

npm install npm test

Contributors

https://github.com/senchalabs/connect/graphs/contributors

Node Compatibility

  • Connect < 1.x - node 0.2
  • Connect 1.x - node 0.4
  • Connect < 2.8 - node 0.6
  • Connect >= 2.8 < 3 - node 0.8
  • Connect >= 3 - node 0.10, 0.12; io.js 1.x, 2.x
时间: 2024-08-14 23:53:05

Connect is a middleware layer for Node.js的相关文章

Node.js的Connect框架的代码重写与改进

Node.js的Connect框架的代码重写与改进 Connect框架简介 Connect框架是建立在Node.js的基本http.server功能之上,帮助实现结构化的web服务器逻辑的框架.Connect框架建立在两个重要的设计模式之上. 1) 责任链模式 在处理web请求时常需要作分派处理.例如,ASP.NET MVC支持按照请求参数将处理分派至某个Controller类的某个Action方法,以及根据Action方法的返回结果类型分派不同的结果操作(如ViewResult.JsonRes

Node.js笔记(0001)---connect模块

首先来看这一部分代码 1 /** 2 * Created by bsn on 14-7-1. 3 */ 4 var connect = require('connect'); 5 6 var app = connect(); 7 function hello(req, res, next) { 8 console.log(req.url); 9 res.end('hello bsn'); 10 next(); 11 } 12 13 function helloAgain(req, res) {

在node.js的命令行里通过npm安装phonegap出现connect etimeout问题

在node.js的命令行里(Node.js command prompt)执行npm install -g phonegap命令安装phonegap3.0,出现etimeout问题. 我是通过设置代理访问外网,而命令行里没有设置代理,所以在命令行里无法连接网络,无法安装成功.出现这种情况需要在命令行里设置代理. windows的命令行下设置网络代理,在命令行下,执行如下命令: set http_proxy=http://proxy.com:port/ set http_proxy_user=us

使用Node.js完成的第一个项目的实践总结

http://blog.csdn.net/yanghua_kobe/article/details/17199417 项目简介 这是一个资产管理项目,主要的目的就是实现对资产的无纸化管理.通过为每个资产生成二维码,来联合移动终端完成对资产的审核等.这个项目既提供了Web端的管理界面也提供移动端(Andorid)的资产审核.派发等相关功能.我们用Node.js构建该项目的Web端以及移动端的Serveice API. 项目主框架:Express 简介 Express 是一个非常流行的Node.js

Node.js Express 框架学习

转载:http://JavaScript.ruanyifeng.com/nodejs/express.html#toc0 感觉很牛的样子,不过觉得对初学者没太大用,里面很多例子用的api都没有详细的说明.为了学习备份,所以拷贝过来. Express框架 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 目录 概述 运行原理 底层:http模块 对http模块的再包装 什么是中间件 use方法 Express的方法 all方法和HTTP动词方法 set方法 response

[Node.js]在windows下不得不防的小错误

TypeError: Arguments to path.join must be strings at f (path.js:204:15) at Object.filter (native) at exports.join (path.js:209:40) at exports.send (E:\nodejs\demo\socket.io-express\node_modules\express\node_modules\connect\lib\middleware\static.js:12

Express 4.x Node.js的Web框架----《转载》

本文使用node.js v0.10.28 + express 4.2.0 1 Express概述 Express 是一个简洁而灵活的node.js的MVC Web应用框架,提供一系列强大特性创建各种Web应用. Express 不对 node.js 已有的特性进行二次抽象,我们只是在它之上扩展了Web应用所需的功能. Expressd底层由Node.js的HTTP模块实现. 1.1 express 4.x 安装 express 4.x与之前的版本有了许多的变化,书里和网上的很多方法都不再适用.学

推荐15个 Node.js 开发工具

Node.js 越来月流行,这个基于 Google V8 引擎建立的平台, 用于方便地搭建响应速度快.易于扩展的网络应用.在本文中,我们列出了2015年最佳的15个 Node.js 开发工具.这些工具对于刚刚开始学习 Node.js 的新手开发者非常有帮助.如果你知道任何其他有用的 Node.js 资源,请让我们知道. 1. IO.js JavaScript I/O is an npm compatible platform that was originally based on Node.j

2015年最佳的15个 Node.js 开发工具

Node.js 越来月流行,这个基于 Google V8 引擎建立的平台, 用于方便地搭建响应速度快.易于扩展的网络应用.在本文中,我们列出了2015年最佳的15个 Node.js 开发工具.这些工具对于刚刚开始学习 Node.js 的新手开发者非常有帮助.如果你知道任何其他有用的 Node.js 资源,请让我们知道. 您可能感兴趣的相关文章 Web 前端开发人员和设计师必读精华文章推荐 精心挑选的优秀jQuery Ajax分页插件和教程 12个让人惊叹的的创意的 404 错误页面设计 让网站动