新发布了1个koa-router-namespace模块,是对koa-router模块的增强,Nodejs开发者欢迎试用

新发布了1个koa-router-namespace模块,是对koa-router模块的增强,Nodejs开发者欢迎试用

github地址: koa-router-namespace 复制下大致的用法,详细说明请见上面的地址。

app.namespace('/users', function() {
	// 匹配 GET /users
	app.get('/', function *() {
		
	})
	
	// path为/时可省略,直接传handler即可
	// 匹配 POST /users
	app.post(function *() {
	
	})
	
	// 匹配 GET /users/:id
	app.get('/:id', function *() {
	
	})
	
	// 匹配 DELETE /users/:id
	app.del('/:id', function *() {
	
	})
	
	// 匹配 PUT /users/:id
	app.put('/:id', function *() {
	
	})
	
	// 支持嵌套
	app.namespace('/:id', function() {
		// 匹配 GET /users/:id/contacts
		app.get('/contacts', function *() {
			
		})
	})
	
	// route method支持数组,这个本身koa-router也是支持的,只是我看他文档没好像没写
	// namespace不支持数组
	// 匹配 GET /users/:id 和 GET /users/:id/profile
	app.get(['/:id', '/:id/profile'], function *() {
	
	})
})

7 回复

新发布了1个koa-router-namespace模块,是对koa-router模块的增强,Nodejs开发者欢迎试用

我们刚刚发布了一个名为 koa-router-namespace 的新模块,它是在 koa-router 模块基础上的一个增强版本。通过使用这个模块,你可以更方便地组织和管理你的路由,使其更具可读性和可维护性。

GitHub 地址:

koa-router-namespace

示例代码:

const Koa = require('koa');
const app = new Koa();
const router = require('koa-router-namespace')(app);

app.namespace('/users', function() {
    // 匹配 GET /users
    this.get('/', function *() {
        this.body = 'List all users';
    });
    
    // path为/时可省略,直接传handler即可
    // 匹配 POST /users
    this.post(function *() {
        this.body = 'Create a new user';
    });
    
    // 匹配 GET /users/:id
    this.get('/:id', function *() {
        const id = this.params.id;
        this.body = `Get user with ID ${id}`;
    });
    
    // 匹配 DELETE /users/:id
    this.del('/:id', function *() {
        const id = this.params.id;
        this.body = `Delete user with ID ${id}`;
    });
    
    // 匹配 PUT /users/:id
    this.put('/:id', function *() {
        const id = this.params.id;
        this.body = `Update user with ID ${id}`;
    });
    
    // 支持嵌套
    this.namespace('/:id', function() {
        // 匹配 GET /users/:id/contacts
        this.get('/contacts', function *() {
            const id = this.params.id;
            this.body = `Get contacts for user with ID ${id}`;
        });
    });
    
    // route method支持数组,这个本身koa-router也是支持的,只是我看他文档没好像没写
    // namespace不支持数组
    // 匹配 GET /users/:id 和 GET /users/:id/profile
    this.get(['/:id', '/:id/profile'], function *() {
        const id = this.params.id;
        this.body = `Get profile for user with ID ${id}`;
    });
});

app.use(router.routes()).use(router.allowedMethods());

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

说明:

  1. Namespace 路由:

    • 使用 app.namespace 方法来定义一个命名空间,可以将一组相关的路由组合在一起。
    • 在示例中,/users 是一个命名空间,所有的子路由都会被挂载在这个路径下。
  2. 路由方法:

    • 可以使用各种HTTP方法(如 get, post, del, put)来定义路由。
    • 如果路由路径以 /:id 形式出现,可以通过 this.params.id 获取动态参数。
  3. 嵌套路由:

    • 你可以在一个命名空间内部再定义另一个命名空间或路由,这使得路由结构更加清晰。
  4. 数组形式:

    • route 方法支持传入数组,这样可以匹配多个路径。

通过这种方式,你可以更好地组织你的路由,使代码更加整洁和易于维护。希望这个新模块能帮助到大家!


从 rails 社区过来 node 的时候我也喜欢楼主 router namespace 这东西。

现在我更喜欢平铺的,一维的写法。一目了然啊。

嗯,这个我一般在后台管理系统用的比较多,因为后台管理功能绝大部分就是增删改查,在这基础上再稍微封装下也方便重用。

koa 现在用得多么

你可以试试,挺好用的。比callback要直观,很贴近同步的开发方式。

就是为了少写/users?

koa-router-namespace 是一个对 koa-router 模块的增强,它允许你通过定义命名空间来更方便地组织路由。这样可以让你的代码结构更加清晰,并且减少重复代码。

以下是使用 koa-router-namespace 的示例代码:

const Koa = require('koa');
const Router = require('@koa/router');
const routerNamespace = require('koa-router-namespace');

const app = new Koa();
const router = new Router();

app.use(routerNamespace('/users', router, function() {
    // 匹配 GET /users
    this.get('/', async ctx => {
        ctx.body = 'List of users';
    });

    // 匹配 POST /users
    this.post('/', async ctx => {
        ctx.body = 'User created';
    });

    // 匹配 GET /users/:id
    this.get('/:id', async ctx => {
        ctx.body = `User with id ${ctx.params.id}`;
    });

    // 匹配 DELETE /users/:id
    this.delete('/:id', async ctx => {
        ctx.body = `User with id ${ctx.params.id} deleted`;
    });

    // 匹配 PUT /users/:id
    this.put('/:id', async ctx => {
        ctx.body = `User with id ${ctx.params.id} updated`;
    });

    // 支持嵌套
    this.namespace('/:id', function() {
        // 匹配 GET /users/:id/contacts
        this.get('/contacts', async ctx => {
            ctx.body = `Contacts for user with id ${ctx.params.id}`;
        });
    });

    // 路由方法支持数组
    this.get(['/:id', '/:id/profile'], async ctx => {
        ctx.body = `Profile for user with id ${ctx.params.id}`;
    });
}));

app.listen(3000);

在这个例子中,我们创建了一个 /users 命名空间,并定义了多个路由处理函数。这样可以更方便地组织代码,并且提高可读性和可维护性。希望这可以帮助到你!

回到顶部