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:
cookieParserlimitmultipartquerystaticCache
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都没有详细的说 ...
随机推荐
- Oracle数据库 控制文件
一.概念控制文件的主要任务是管理数据库的状态以及描述数据库的物理结构 二.所含有的信息1.数据库名2.数据库标识符(DBID)3.数据库创建时间戳4.数据库字符集5.数据文件信息6.临时文件信息7.在 ...
- WPF MVVM模式下实现ListView下拉显示更多内容
在手机App中,如果有一个展示信息的列表,通常会展示很少一部分,当用户滑动到列表底部时,再加载更多内容.这样有两个好处,提高程序性能,减少网络流量.这篇博客中,将介绍如何在WPF ListView中实 ...
- 第二篇:SOUI源码的获取及编译
源代码的获取 SOUI的源码采用SVN管理. SVN:http://code.taobao.org/svn/soui2 这里主要包含两个目录:trunk 及 third-part. trunk目录保存 ...
- Android入门开发之SD卡读写操作(转)
SD卡的读写是我们在开发android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 String sdCardRoot = Environment.getE ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
- 利用canvas实现抽奖转盘---转载别人的
功能需求 转盘要美观,转动效果流畅. 转盘上需要显示奖品图片,并且奖品是后台读取的照片和名字. 转动动画完成后要有相应提示. 获取的奖品具体算法在数据库里操作,前端只提供最后的效果展示. 知识要点 ...
- Android自动化测试之Monkey Test 安装(二)
因为Monkey Test是在eclipse上执行的,所以玩monkey test的时候要先配置安卓开发环境 一.Android开发环境搭建指南 1.安装JDK JDK下载链接:http://www. ...
- poj 2115 Looooops
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23637 Accepted: 6528 Descr ...
- a标签 打电话 发邮件
打电话<a href=”tel:010-13220163333″>13220163333</a> 发邮件<a href=”mailto:sb@you.com”>发送 ...
- 手机页面的meta标签
<meta charset="utf-8"/><meta name="viewport" content="width=device ...