我正在尝试在我的 Express js 应用程序中使用类实现路由
Controller
class User {
constructor (){
this.username = 'me';
}
getUsername(req,res){
res.json({
'name':this.name
});
}
}
export default User;
在我的 route
import express from 'express'
import User from './controller'
const router = express.Router();
const user = new User();
router('/',user.getUsername.bind(user));
export default UserRoute
但是我收到了这个错误
req.next = next; ^
TypeError: Cannot create property 'next' on string '/' at Function.handle (/var/accubits-workspace/express-es6/node_modules/express/lib/router/index.js:160:12)
您没有在路由器对象上使用任何方法,您需要诸如 get
、 post
、 put
、 delete
或其他 http/https 动词或 use
来创建中间件。
例如
router.VERB("/", ...);
我是一名优秀的程序员,十分优秀!