gpt4 book ai didi

javascript - 具有 es6 类 : Cannot create property 'next' on string '/' 的快速路线

转载 作者:太空宇宙 更新时间:2023-11-04 03:21:48 26 4
gpt4 key购买 nike

我正在尝试在我的 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)

最佳答案

您没有在路由器对象上使用任何方法,您需要诸如 getpostputdelete 或其他 http/https 动词或 use 来创建中间件。

例如

router.VERB("/", ...);

关于javascript - 具有 es6 类 : Cannot create property 'next' on string '/' 的快速路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49335352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com