Connect 是一个可扩展的 HTTP 服务器框架,用于在 node 中被称为插件的中间件。
- 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({extended: false}));
-
- // 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);
Connect 是一个简单的框架,可以将各种 中间件 粘合在一起来处理请求。
- $ npm install connect
主要组件是一个 Connect 应用程序。这将存储所有添加的中间件,它本身就是一个函数。
- var app = connect();
Connect 的核心是 using 中间件。中间件被添加为 stack,其中传入的请求将逐个执行每个中间件,直到中间件不在其中调用 next()。
- app.use(function middleware1(req, res, next) {
- // middleware 1
- next();
- });
- app.use(function middleware2(req, res, next) {
- // middleware 2
- next();
- });
该.use()方法还采用与传入请求 URL 开头匹配的可选路径字符串。这允许基本路由。
- 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-handling 中间件的特殊情况。有中间件,该函数只需要 4 个参数。当中间件将错误传递给 时next,应用程序将继续查找在该中间件之后声明的错误中间件并调用它,跳过该中间件上方的任何错误中间件和下方的任何非错误中间件。
- // regular middleware
- app.use(function (req, res, next) {
- // i had an error
- next(new Error('boom!'));
- });
-
- // error middleware for errors that occurred in middleware
- // declared before this
- app.use(function onerror(err, req, res, next) {
- // an error occurred!
- });
最后一步是在服务器中实际使用 Connect 应用程序。该 .listen() 方法可以方便地启动 HTTP 服务器(与 您正在运行的 Node.js 版本中的http.Server'slisten方法相同)。
- var server = app.listen(port);
应用程序本身实际上只是一个带有三个参数的函数,因此它也可以.createServer()在 Node.js 中传递。
- var server = http.createServer(app);
Connect/Express 团队正式支持这些中间件和库:
其中大部分是其 Connect 2.x 等效版本的确切端口。主要的例外是cookie-session.
Connect/Express 团队不再支持以前包含在 Connect 中的一些中间件,它们被替代模块替换,或者应该被更好的模块取代。请改用以下替代方法之一:
点击 http-framework 查看其他兼容的中间件。
Connect API 非常简约,足以创建一个应用程序并添加一系列中间件。
当connect需要该模块时,将返回一个函数,该函数将在调用时构建一个新的应用程序。
- // require module
- var connect = require('connect')
-
- // create app
- var app = connect()
本app本身就是一个功能。这只是 的别名app.handle。
调用该函数将针对给定的 Node.js http 请求 ( req) 和响应 ( res) 对象运行中间件堆栈。out 如果中间件堆栈未处理请求(或错误),则可以提供一个可选函数,该函数将被调用。
启动应用程序侦听请求。此方法将在内部创建一个 Node.js HTTP 服务器并调用.listen()它。
这是server.listen()运行的 Node.js 版本中方法的别名,因此请参阅 Node.js 文档以了解所有不同的变体。最常见的签名是app.listen(port).
在应用程序上使用一个函数,该函数代表一个中间件。该函数将按调用顺序为每个请求app.use调用。该函数使用三个参数调用:
- app.use(function (req, res, next) {
- // req is the Node.js http request object
- // res is the Node.js http response object
- // next is a function to call to invoke the next middleware
- })
除了计划函数之外,fn参数还可以是 Node.js HTTP 服务器实例或另一个 Connect 应用程序实例。
在应用程序上使用一个函数,该函数代表一个中间件。该函数将针对每个请求调用,其中 URL(req.url属性)route按app.use调用顺序以给定字符串开头。该函数使用三个参数调用:
- app.use('/foo', function (req, res, next) {
- // req is the Node.js http request object
- // res is the Node.js http response object
- // next is a function to call to invoke the next middleware
- })
除了计划函数之外,fn 参数还可以是 Node.js HTTP 服务器实例或另一个 Connect 应用程序实例。
在 route 总是在一个路径分隔(终止 /)或点(.)字符。这意味着,给定的路线 /foo/ 和 /foo 是相同的,都将匹配的URL的请求 /foo,/foo/,/foo/bar,和 /foo.bar,但不匹配的URL的请求/foobar。
所述 route 在不区分大小写的方式相匹配。
为了使中间件更容易编写为与 route 无关,当 fn 被调用时,req.url 将被更改以删除 route 部分(并且原始将作为 可用 req.originalUrl)。例如,如果fn在 route 使用/foo,则请求/foo/bar将fn使用req.url === '/bar' and调用req.originalUrl === '/foo/bar'。