Connect is a middleware layer for Node.js
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:
- body-parser - previous
bodyParser
,json
, andurlencoded
. You may also be interested in: - compression - previously
compress
- connect-timeout - previously
timeout
- cookie-parser - previously
cookieParser
- cookie-session - previously
cookieSession
- csurf - previously
csrf
- errorhandler - previously
error-handler
- express-session - previously
session
- method-override - previously
method-override
- morgan - previously
logger
- response-time - previously
response-time
- serve-favicon - previously
favicon
- serve-index - previously
directory
- serve-static - previously
static
- vhost - previously
vhost
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:
cookieParser
limit
multipart
query
staticCache
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
- node0.2
- Connect
1.x
- node0.4
- Connect
< 2.8
- node0.6
- Connect
>= 2.8 < 3
- node0.8
- Connect
>= 3
- node0.10
,0.12
; io.js1.x
,2.x
Connect is a middleware layer for Node.js的更多相关文章
- Node.js + React + MongoDB 实现 TodoList 单页应用
之前用 Ant Design 开发了一个项目,因此对 React 的特性有了一定的了解,React 使用封装组件的思想,组件各自维护自己的状态和 UI, 组件之间通过 props 传递数据和方法.当状 ...
- [Node.js] 09 - Connect with Database
简介两个数据库: Node.js 连接 MySQL Node.js 连接 MongoDB Node.js 连接 MySql 导入已有数据库: unsw@unsw-UX303UB$ mysql -u r ...
- node.js Error: connect EMFILE 或者 getaddrinfo ENOTFOUND
Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' Error: c ...
- Node.js Web 开发框架大全《中间件篇》
这篇文章与大家分享优秀的 Node.js 中间件模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处 ...
- Node.js 入门手册:那些最流行的 Web 开发框架
这篇文章与大家分享最流行的 Node.js Web 开发框架.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编 ...
- 使用Node.js完成的第一个项目的实践总结
http://blog.csdn.net/yanghua_kobe/article/details/17199417 项目简介 这是一个资产管理项目,主要的目的就是实现对资产的无纸化管理.通过为每个资 ...
- Practical Node.js (2018版) 第7章:Boosting Node.js and Mongoose
参考:博客 https://www.cnblogs.com/chentianwei/p/10268346.html 参考: mongoose官网(https://mongoosejs.com/docs ...
- [转]使用Node.js完成的第一个项目的实践总结
本文转自:http://blog.csdn.net/yanghua_kobe/article/details/17199417 https://github.com/yanghua/FixedAsse ...
- Node.js Express 框架学习
转载:http://JavaScript.ruanyifeng.com/nodejs/express.html#toc0 感觉很牛的样子,不过觉得对初学者没太大用,里面很多例子用的api都没有详细的说 ...
随机推荐
- PHP二维数组去除重复,重复值相加
$arr = array( array('id' => 122, 'name' => '张三', 'amount' => '1'), array('id' => 123, 'n ...
- php几个常用的概率算法(抽奖、广告首选)
做网站类的有时会弄个活动什么的,来让用户参加,既吸引用户注册,又提高网站的用户活跃度.同时参加的用户会获得一定的奖品,有100%中奖的,也有按一定概率中奖的,大的比如中个ipad.iphone5,小的 ...
- ASP.NET 5探险(6):升级ASP.NET 5到beta6
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:微软根据ASP.NET 5的路线图如期发布了beta6,现在我们就来说说beta5升级 ...
- linux下java环境配置
非常简单的三行命令就搞定了! $ sudo add-apt-repository ppa:webupd8team/java$ sudo apt-get update$ sudo apt-get ins ...
- ios编码转换 国标 UTF-8
我们知道,使用NSURLConnection的代理方法下载网页,存到一个NSData中, NSMutableData *pageData; [pageData appendData:data]; 如果 ...
- 浅谈JSON.parse()、JSON.stringify()和eval()的作用
(1)JSON.parse 函数 var json = '{"name":"GDT","age":23,"University&q ...
- OpenGL的消隐与双缓冲
首先是大家可能已经发现,在我们之前提到的所有例子中,在图形的旋转过程中整个图形都有一定程度的闪烁现象,显得图形的过渡极不平滑,这当然不是我们所要的效果,幸好opengl 支 持一个称为双缓存的技术,可 ...
- 分布式服务框架 Zookeeper -- 管理分布式环境中的数据
转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/index.html Zookeeper 分布式服务框架是 Apa ...
- loadrunner选择执行哪个Action
深圳湖北籍软件测试群 275212937
- Codeforces Round #355 (Div. 2)-C
C. Vanya and Label 题目链接:http://codeforces.com/contest/677/problem/C While walking down the street Va ...