组织Node.js中的路由 [英] Organize routes in Node.js

查看:105
本文介绍了组织Node.js中的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始看Node.js.另外我正在使用Express。
我有一个问题 - 我如何组织Web应用程序路由?所有这些例子都将这些 app.get / post / put()处理程序放在app.js中,它的工作原理很好。这是很好的,但如果我有一些不仅仅是一个简单的HW博客?可以这样做:

  var app = express.createServer(); 
app.get('/ module-a / *',require('./ module-a')。urls);
app.get('/ module-b / *',require('./ module-b')。urls);

  // file:module-a.js 
urls.get('/:id',function(req,res){...}); //< - 假设这是/ module-a / 1的处理程序

换句话说 - 我'd喜欢像Django的URLConf,但在Node.js。

解决方案

查看这里的例子:



https://github.com/visionmedia/express/树/主/示例



'mvc'和'route-separation'可能会有帮助。


I start to look at Node.js. Also I'm using Express. And I have a question - how can I organize web application routes? All examples just put all this app.get/post/put() handlers in app.js and it works just fine. This is good but if I have something more than a simple HW Blog? Is it possible to do something like this:

var app = express.createServer();
app.get( '/module-a/*', require('./module-a').urls );
app.get( '/module-b/*', require('./module-b').urls );

and

// file: module-a.js
urls.get('/:id', function(req, res){...}); // <- assuming this is handler for /module-a/1

In other words - I'd like something like Django's URLConf but in Node.js.

解决方案

Check out the examples here:

https://github.com/visionmedia/express/tree/master/examples

'mvc' and 'route-separation' may be helpful.

这篇关于组织Node.js中的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆