- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
使用 Express/Connect,我可以设置任何一种格式的中间件功能:
function(req, res, next) // first argument will be a request
或者
function(err, req, res, next) // first argument will be an error
从 Express、Connect 退回到基本的 JavaScript:我不明白这怎么可能有一个可选的第一个参数?
如何表达知道我的函数将首先接受一个 err
对象?我认为要使这成为可能,结构必须如下所示:
function(req, res, next, err)
我在这里缺少一些基本的东西吗?是否可以查询一个函数需要多少个参数?
编辑:感谢以下回答。但是中间件函数是传递给express的,所以arguments
变量是无效的。虽然 length
是正确的......我想我已经想通了,最好确认是否是这种情况。示例如下:
var fn;
fn = function (one, two) {};
console.log(fn.length); // 2
fn = function (one, two, three) {};
console.log(fn.length); // 3
最佳答案
I think I have figured it out, would be good to have confirmation to whether this is the case
var fn;
fn = function (one, two) {};
console.log(fn.length); // 2
fn = function (one, two, three) {};
console.log(fn.length); // 3
是的,没错。 Function
实例的 length
属性是它拥有的形式参数(声明的参数)的数量。这隐藏在 Section 13.2 中规范的第 14 步和第 15 步。
因此,调用函数的代码很容易检查 fn.length
并将可选的第一个参数传递给它,或者不传递给它,这取决于它。当然,这确实意味着完全可以编写一个 处理四参数版本的函数,但通过使用 arguments
而不是形参来欺骗框架。所以你不会那样做。 :-)
(很抱歉第一次误读您的问题。)
关于javascript - 这是如何运作的? Express 中使用的可选第一个参数 (err, req, res, next) 或 (req, res, next),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8431186/
我正在使用 express并试图通过管道传输 req到远程服务器进行处理,然后通过 res 将结果通过管道传回客户端.它运行良好,但有时我认为 res由 connect-timeout 的操作结束,导
我正在尝试使用 fetch 进行简单的发布请求: const headers = new Headers({ Accept: 'application/json', 'Content-Type
我偶然发现了一个代码示例 here .引起我注意的行(跳过所有其他行): { ... void *res; ... s = pthread_join(tinfo[tnum].thread_id
我有一个 NodeJS 代码,使用请求模块向服务器发出请求。如果我使用“http.request”,代码工作正常,但在 res.on() 上显示错误使用请求来调用电话。以下是显示错误的部分: cons
我想知道这两种不同的方法在 expressjs 中是否相同? res.statusCode = 500; return res.json({ status: "error" }); 或者 r
我正在尝试在 Node.js 中制作简单的 ftp 客户端。一切正常,但我不知道如何在我的 Jade 模板中编写“singleFile.name”。 app.post('/ftp/login', fu
在frameworks/base下有这个xml文件。 /frameworks/base/core/res/res/layout/simple_dropdown_item_2line.xml 如何在我自
我是 odoo v8 的新手,我无法理解 res_partner 和 res_users 表之间的关系以及与 hr_employee 表之间的关系,它们都相关吗? 最佳答案 res.partner之间
我正在开发一个用于用户登录的小部件。基本上,当他们输入有效的帐户详细信息时,我希望它呈现相同的页面(使用 Handlebars )并声明“帐户已成功验证”之类的内容,然后 3 秒后重定向用户。 这是我
我有这种形式的数据 [ { device_id: '12335', timestamp: '2018-05-14T08:31:23.000Z', temperatur: 21,
我是 android 开发的新手,您可能会从我的问题中看出这一点。我发现我有一个 res/menu 文件夹和一个 res/layout 文件夹。它们都包含每个 Activity 的 XML 文件。但是
当我尝试按照指南中的说明在 res/menu 中创建新的菜单资源文件时,该文件夹不存在。 res/xml 也不是。有人可以告诉我如何解决这个问题吗?我尝试创建文件夹,但它不允许我这样做 最佳答案 如果
调用有什么区别: res.flushBuffer(); 对比 res.getOutputStream().flush(); 这些方法是否刷新相同的缓冲区? 如果是这样,你能告诉我这个缓冲区是如何由
为什么以下不起作用? res.send({ successMessage: 'Task saved successfully.' }); res.redirect('/'); 我基本上需要 A
我正在尝试确定是否可以同时调用 res.send(data) 和 res.render('reports')。 为了进一步详细解释,当我路由到“/reports”时,首先在我的服务器端我对返回 jso
app.get('/p/:tagId', function(req, res) { res.send("tagId is set to " + req.param("tagId")); }); 上
我在使用express.js 时遇到一些问题。我想在第一次请求时渲染我的模板,然后从路由中获取一些数据。我的路由如下所示: app.use(function (req, res, next) { r
我想根据屏幕尺寸加载 dimens.xml,但无论屏幕尺寸如何都使用相同的颜色。 我有以下目录结构 res res/values colors.xml res/values-small d
我有一张图片,rectangle.png。它的尺寸是 75x197。它位于 res/drawable 和 res/drawable-xhdpi。它在像这样的网格布局中显示了很多次(截图来自 Nexus
我很好奇返回响应和仅创建响应的区别。 我见过大量使用 return res.status(xxx).json(x) 的代码示例和res.status(xxx).json(x) . 谁能详细解释一下两者
我是一名优秀的程序员,十分优秀!