【发布时间】:2017-10-03 14:08:19
【问题描述】:
您好,我正在尝试遵循 ES6 语法来创建我的 node.js 应用程序的中间件。
index.js
export default class Middleware {
constructor() {
//do nothing
}
fun1 = (req, res, next) => {
console.log("------------------------------------");
console.log("AAa");
console.log("------------------------------------");
next();
};
fun2 = (req, res, next) => {
console.log("------------------------------------");
console.log("AAa");
console.log("------------------------------------");
next();
};
}
app.js
import Middleware from ".index";
app.use(Middleware);
我收到一个错误,无法将类调用为函数。有谁知道怎么回事?
【问题讨论】:
-
哪一行给出了错误?
-
当我删除
app.use(Middleware);然后它工作 -
这是用express框架吗?
-
调用类的时候要加上
new关键字,试试app.use(new Middleware); -
它告诉你想错了——你不能在这里使用一个类。
app.use()期待具有特定签名的函数。
标签: javascript node.js express ecmascript-6