2025年4月5日 星期六 乙巳(蛇)年 正月初六 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > 网络服务

Connect 可扩展的 HTTP 服务器框架

时间:12-14来源:作者:点击数:7
CDSY,CDSY.XYZ

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 是一个简单的框架,可以将各种 中间件 粘合在一起来处理请求。

安装 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(req, res[, next])

app本身就是一个功能。这只是 的别名app.handle

app.handle(req, res[, out])

调用该函数将针对给定的 Node.js http 请求 ( req) 和响应 ( res) 对象运行中间件堆栈。out 如果中间件堆栈未处理请求(或错误),则可以提供一个可选函数,该函数将被调用。

app.listen([...])

启动应用程序侦听请求。此方法将在内部创建一个 Node.js HTTP 服务器并调用.listen()它。

这是server.listen()运行的 Node.js 版本中方法的别名,因此请参阅 Node.js 文档以了解所有不同的变体。最常见的签名是app.listen(port).

app.use(fn)

在应用程序上使用一个函数,该函数代表一个中间件。该函数将按调用顺序为每个请求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 应用程序实例。

app.use(route, fn)

在应用程序上使用一个函数,该函数代表一个中间件。该函数将针对每个请求调用,其中 URL(req.url属性)routeapp.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/barfn使用req.url === '/bar' and调用req.originalUrl === '/foo/bar'

项目地址:https://github.com/senchalabs/connect

CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐