Express app.get文档 [英] Express app.get documentation

查看:153
本文介绍了Express app.get文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些关于express.js的 app.get 函数的文档。

I am looking for some documentation on the app.get function of express.js.

app.get(
    '/path', 
    middleware(),
    function(req, res) {
        res.redirect('/');
    }
);

上面的例子有三个参数。正常的文档只显示两个。我感兴趣的是这个中间参数,以及如何使用它。

The example above takes three parameters. The normal docs only show two. I'm interested in what this middle param does and how to use it.

推荐答案

code> app.METHOD 文档,其中 get 是支持的HTTP方法之一。

The docs for that are part of the app.METHOD documentation, where get is one of the supported HTTP methods.

第二个可选参数称为中间件(您可以通过一系列中间件功能)。这是一个在第三个参数回调之前调用的函数(实际路由处理程序),中间件函数的职责是允许您的代码遵循DRY(不要重复)原则。

The second, optional parameter, is called middleware (and you can pass an array of middleware functions). This is a function that's called before the third parameter callback (the actual route handler) and the responsibility of a middleware function is to allow your code to follow the DRY (don't repeat yourself) principle.

中间件功能的示例是权限检查,访问验证,会话验证(如果用户未登录,将其带到登录页面)等。

Example of middleware functions are permissions checks, access validations, validation of sessions (if user is not in logged in, take him to a log in page), and such.

由于几个路由可能需要相同的行为,所以您使用中间件,因此您不必多次编写相同的代码。

Since several routes might desire the same behavior, you use a middleware so that you don't have to write the same code several times.

这篇关于Express app.get文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆