Connect with the Connect framework

WBOY
Release: 2023-08-29 18:41:09
Original
1166 people have browsed it

与 Connect 框架连接

Newcomers to NodeJS often find its API difficult to master. Fortunately, many developers have created frameworks that make working with Node.js easier. Connect is such a framework. It sits on top of Node's API and draws the line between comfort and control.

Think of Connect as a middleware stack. For each request, Connect is filtered through middleware layers, each of which gets a chance to handle the HTTP request. When T.J. Holowaychuk announced Connect, he said there were two types of middleware. The first one isFilter.

Filter handles the request but does not respond to it (think server logging).

Another type is theprovider, which responds to requests. You can combine as many middlewares as you want; the request goes through each layer until one of the middlewares responds to the request.


Basic Grammar

First, you need to install the Connect package via npm:

npm install connect
Copy after login

Now create aserver.jsfile and add the following code:

var connect = require("connect");
Copy after login

connectThe variable is a function that returns the new Connect application. So our next step is to create the application:

var app = connect();
Copy after login

You don't need to createappvariables for most applications. The functions involved in creating the application (connect()anduse()) are chainable:

connect() .use(/* middleware */) .use(/* middleware */) .listen(3000);
Copy after login
The

use()function adds a layer of middleware to the application, while thelisten()function tells our application to start accepting the specified port (3000 in this case) connection on.

Let's start with something simple: logging. The code for a Connect application using just the logging middleware is fairly simple:

connect() .use(connect.logger()) .listen(3000);
Copy after login

By default, Node will parse very few incoming requests.

Add this code to your file and start the server by runningnode server.js. Navigate to any path in your browser and ignore the "Unable to get..." result. We're not interested in what the server sends back to the browser; we're interested in the server's logs. Look in the terminal and you will see your request log. Be sure to check out the logger documentation for information on its additional features and customization.

That was a filter; now let's look at a provider. The simplest provider is the static provider; it serves static files in a specified folder. This is its syntax:

.use(connect.static(__dirname + "/public")
Copy after login

You can probably guess the purpose of Node's__dirnamevariable: it is the path to the current directory. This middleware statically serves whatever is in thepublicfolder in the current directory. So createpublic/page.htmland add the

element. Restart the server (node server.js) and navigate tolocalhost:3000/page.htmlin your browser. You should renderpage.htmlin the browser.

Now let’s take a quick look at some of Connect’s other middleware options.


Parse the request body

By default, Node will parse few incoming requests, but if you need to handle more complex situations, you can combine multiple different filters to parse the requests. There are four filters:

  • connect.json()Parse the JSON request body (wherecontent-typeisapplication/json).
  • connect.urlencoded()Parsex-ww-form-urlencodedRequest body.
  • connect.multipart()Parsemultipart/form-dataRequest body.
  • connect.bodyParser()is a shortcut to enable all three of the above features.

Using any of these filters will give you access to the parsed body viarequest.body(we'll discuss how to obtain therequestobject shortly).

I think these filters are a great example of how fine-grained control can be achieved through Connect. You can simplify your application with very little processing.


Parsing Cookies and Sessions

Cookies and sessions are an important part of any web application, and there are several middlewares that can help manage them.connect.cookieParser()will parse the cookie for you, and you can retrieve the cookie and its value through therequest.cookiesobject. This will be more useful if you add theconnect.session()filter to your application. This filter requires a cookie parser to be in place. Here is a small example:

connect() .use(connect.cookieParser()) .use(connect.session({ secret: 'some secret text', cookie: { maxAge: 30000 }})) .use(function(req, res) { var sess = req.session, url = req.url.split("/"); if (url[1] == "name" && url[2]) { sess.name = url[2]; res.end("name saved: " + url[2]); } else if (sess.name) { res.write("session-stored name: " + sess.name); res.end("stored for another: " + (sess.cookie.maxAge / 1000) + " seconds"); } else { res.end("no stored name; go to /name/{name} to save a name"); } }).listen(3000);
Copy after login

Every middleware function you write needs to pass the request to thenextlayer or respond to the request.

AftercookieParserwe include thesessionfilter and pass it two options:

  • secret创建一个签名 cookie,用于跟踪会话。
  • cookie.maxAge定义其生命周期(以毫秒为单位);此代码中的 30000 是 30 秒。

在最后的use()调用中,我们传递一个响应请求的函数。我们使用request对象中的两个属性:req.session用于会话数据,req.url用于请求 URL。

如果应用程序收到对/name/some_name的请求,则它将值some_name存储在req.session.name中。会话中存储的任何内容都可以在会话期间的后续请求中检索。对/name/other发出的任何请求都会替换会话变量,对其他 URL 的任何请求都会输出会话变量的值和会话剩余时间。

因此,您可以导航到localhost:3000/name/your_name,然后转到localhost:3000查看your_name。刷新页面几次并观察秒数倒计时。当会话过期时,您将看到默认的“未存储名称”消息。

我提到cookieParser过滤器必须位于session之前。

包含的顺序对于中间件很重要,因为请求是按顺序从一层传递到另一层的。

由于session需要解析后的cookie数据,因此请求必须先经过cookieParser,然后再经过session

我可以解释所有其他内置中间件,但在我们编写自己的代码与 Connect 交互之前,我只会再提一些。

  • compress:Gzip压缩中间件
  • basicAuth:基本http认证
  • 目录:列出中间件的目录
  • errorHandler:灵活的错误处理程序

编写自己的中间件

您刚刚学习了如何使用 Connect 编写自己的代码。这是基本语法:

.use(function (req, res, next) { })
Copy after login

函数的三个参数很重要;它们提供了通往外部世界的通道。req参数当然是请求对象,res是响应。第三个参数next是使函数在中间件堆栈中正常运行的关键。它是一个将请求传递到堆栈中的下一个中间件的函数。请参阅此示例:

connect() .use(function (req, res, next) { if (req.method === 'POST') { res.end("This is a POST request"); } else { next(); } }) .use(function (req, res) { res.end("This is not a POST request (probably a GET request)"); }).listen(3000);
Copy after login

这段代码使用了两个中间件函数。第一个函数检查请求方法以查看是否是 POST 请求。如果是,它就会这样回应。否则,我们调用next()并将请求传递给下一个函数,该函数无论如何都会响应。使用curl在终端中测试两层:

$ curl http://localhost:3000 This is not a POST request (probably a GET request) $ curl -X POST http://localhost:3000 This is a POST request
Copy after login

如果您不喜欢该终端,请尝试这个有用的 Chrome 插件。

重要的是要记住,您编写的每个中间件函数都需要将请求传递到next层或响应请求。如果您的函数分支(通过 if 语句或其他条件),您必须确保每个分支都会传递请求或响应请求。如果您的应用程序在浏览器中挂起,可能是因为您忘记在某个时刻调用next()

现在,那些requestresponse参数怎么样?这些与您在使用“原始”节点服务器时收到的请求和响应对象完全相同:

require("http").createServer(function (req, res) { // ... }).listen(3000);
Copy after login

如果您以前没有使用过 Node 的服务器 API,让我向您展示可以用它做什么。


请求对象

request对象实际上是一个http.IncomingMessage对象,其重要属性如下::

  • req.method告诉您使用了哪种 HTTP 方法。
  • req.url告诉您请求的是哪个 URL。
  • req.headers是一个包含标头名称和值的对象。
  • req.query是一个包含查询字符串中任何数据的对象(要解析它,您需要connect.query()中间件)。
  • req.body是表单数据的对象(您需要一些正文解析中间件)。
  • req.cookies是cookie数据的对象(需要cookie解析)。
  • req.session是会话数据的对象(同样,您需要 cookie 解析和会话中间件)

您可以使用以下代码查看所有这些工作:

connect() .use(connect.query()) // gives us req.query .use(connect.bodyParser()) // gives us req.body .use(connect.cookieParser()) // for session .use(connect.session({ secret: "asdf" })) // gives us req.session .use(function (req, res) { res.write("req.url: " + req.url + "\n\n"); res.write("req.method: " + req.method + "\n\n"); res.write("req.headers: " + JSON.stringify(req.headers) + "\n\n"); res.write("req.query: " + JSON.stringify(req.query) + "\n\n"); res.write("req.body: " + JSON.stringify(req.body) + "\n\n"); res.write("req.cookies: " + JSON.stringify(req.cookies) + "\n\n"); res.write("req.session: " + JSON.stringify(req.session)); res.end(); }).listen(3000);
Copy after login

要查看其中每个值的某些内容,您需要使用查询字符串将一些数据发布到 URL。以下内容应该足够了:

curl -X POST -d "name=YourName" "http://localhost:3000/some/url?some=data"
Copy after login

通过这七个属性,您几乎可以管理收到的任何请求。我不认为经常使用预告片(根据我的经验,我从未见过它们),但是如果您希望在请求中使用预告片,则可以使用req.trailers(预告片就像标头一样,但位于正文之后)。

那么,你的回应呢?


响应对象

原始响应对象不提供库(如 Express)为您提供的便利。例如,您无法通过对预制模板的简单渲染调用来响应 - 至少默认情况下不能。响应中的假设很少,因此您需要填写所有小细节。

我们将从状态代码和响应标头开始。您可以使用writeHead()方法一次性设置所有这些。以下是 Node 文档中的示例:

var body = 'hello world'; response.writeHead(200, { 'Content-Length': body.length, 'Content-Type': 'text/plain' });
Copy after login

如果需要单独设置header,可以使用setHeader()方法:

connect() .use(function (req, res) { var accept = req.headers.accept.split(","), body, type; console.log(accept); if (accept.indexOf("application/json") > -1) { type = "application/json"; body = JSON.stringify({ message: "hello" }); } else if (accept.indexOf("text/html") > -1) { type = "text/html"; body = "

Hello!

"; } else { type = "text/plain"; body = "hello!"; } res.statusCode = 200; res.setHeader("Content-Type", type); res.end(body); }).listen(3000);
Copy after login

将此代码添加到文件中,启动服务器并从浏览器请求它。你有 HTML!现在运行:

curl http://localhost:3000
Copy after login

您将收到纯文本。对于 JSON,请尝试以下操作:

curl -H "accept:application/json" http://localhost:3000
Copy after login

全部来自同一个 URL!

如果您需要知道已经设置了哪些标头,请使用res.getHeader(name)。您还可以使用res.removeHeader(name)删除标头。

当然,没有正文的响应是没有用的。正如您在本教程中所看到的,您可以使用res.write()方法将数据块写入正文。它接受字符串或缓冲区对象作为参数。如果是字符串,第二个参数是编码类型(默认为utf8)。

res.end()方法关闭主体,但您可以向其传递数据以写入响应流。这在您只需要输出一行的情况下非常有用。


第三方中间件

在普通的旧 Node 和 Connect 中响应较大的 HTML 主体有些困难。这是将第三方中间件加入其中的好地方。您可以在 Connect Github wiki 上找到第三方中间件的列表。例如,我们将使用 connect-jade 包,它允许我们渲染 jade 视图。

首先,安装connect-jade

npm install connect-jade
Copy after login

接下来,需要并将其添加为中间件。您需要设置一些默认值:

var connect = require("connect"), connectJade = require("connect-jade"); connect() .use(connectJade({ root: __dirname + "/views", defaults: { title: "MyApp" } })) .use(function (req, res) { res.render("index", { heading: "Welcome to My App" }); }).listen(3000);
Copy after login

将根目录设置为包含视图文件的目录。您还可以设置defaults;这些是每个视图中可用的变量,除非我们稍后在调用render()时覆盖它们。

此代码中的最后一个函数调用res.render()。该方法由connect-jade包提供。

它接受的第一个参数是要渲染的视图的名称。

它是视图的路径,我们在添加中间件时定义的路径,jade 文件扩展名。对于这段代码,我们需要一个views/index.jade模板来渲染。我们会保持简单:

html head title= title body h1= heading
Copy after login

如果您不熟悉jade,我们会缩进标签名称来创建HTML 结构。等号检索 JavaScript 变量的值。这些变量来自我们设置的defaults,加上传递给res.render()的(可选)第二个参数对象。

还有许多其他第三方中间件,但它们的工作原理彼此相似。您可以通过 npm 安装它们、需要它们并将它们付诸实践。


模块作为中间件

如果你深入研究 Connect 的工作原理,你会发现每一层实际上都是一个 Node 模块——这是一个非常智能的设计。如果您将 Connect 用于大型应用程序,那么以 Node 模块格式编写代码将是理想的选择。您可能有一个app.js文件,如下所示:

// app.js module.exports = function (req, res, next) { res.end("this comes from a module"); };
Copy after login

在你的server.js中:

var connect = require("connect"), app = require("./app"); connect() .use(app) .listen(3000);
Copy after login

结论

如果您想要一个适合初学者的库来轻松构建大型 Web 应用程序,那么 Connect 不是您的解决方案。 Connect 旨在成为原始 Node API 之上的一个薄层,让您可以完全控制服务器应用程序。如果你想要更多,我推荐 Express(顺便说一下,是同一个人做的)。除此之外,Connect 是一个出色的、可扩展的 Node Web 应用程序库。

The above is the detailed content of Connect with the Connect framework. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!