在 Node.js 中组织路由

发布于 2024-10-10 17:55:35 字数 533 浏览 6 评论 0原文

我开始关注 Node.js。我也正在使用 Express。 我有一个问题 - 如何组织 Web 应用程序路由?所有示例都将所有 app.get/post/put() 处理程序放入 app.js 中,并且工作得很好。这很好,但如果我有的不仅仅是一个简单的硬件博客?是否可以做这样的事情:

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){...}); // <- assuming this is handler for /module-a/1

- 我想要类似 Django 的 URLConf 但在 Node.js 中的东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

淡淡離愁欲言轉身 2024-10-17 17:55:35

我在 ´Smashing Node.js: JavaScript 中找到了一个简短的示例到处都是我真正喜欢的。

通过将 module-amodule-b 定义为其自己的 Express 应用程序,您可以使用 connect app.use( ) :

module-a.js

module.exports = function(){
  var express = require('express');
  var app = express();

  app.get('/:id', function(req, res){...});

  return app;
}();

module-b.js

module.exports = function(){
  var express = require('express');
  var app = express();

  app.get('/:id', function(req, res){...});

  return app;
}();

app.js

var express = require('express'),
    app = express();

app.configure(..);

app.get('/', ....)
app.use('/module-a', require('./module-a'));    
app.use('/where/ever', require('./module-b'));    

app.listen(3000);

这将为您提供路线

localhost:3000/
localhost:3000/module-a/:id
localhost:3000/where/ever/:id

I found a short example in ´Smashing Node.js: JavaScript Everywhere´ that I really liked.

By defining module-a and module-b as its own express applications, you can mount them into the main application as you like by using connects app.use( ) :

module-a.js

module.exports = function(){
  var express = require('express');
  var app = express();

  app.get('/:id', function(req, res){...});

  return app;
}();

module-b.js

module.exports = function(){
  var express = require('express');
  var app = express();

  app.get('/:id', function(req, res){...});

  return app;
}();

app.js

var express = require('express'),
    app = express();

app.configure(..);

app.get('/', ....)
app.use('/module-a', require('./module-a'));    
app.use('/where/ever', require('./module-b'));    

app.listen(3000);

This would give you the routes

localhost:3000/
localhost:3000/module-a/:id
localhost:3000/where/ever/:id
清君侧 2024-10-17 17:55:35

查看此处的示例:

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

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

Check out the examples here:

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

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

抱着落日 2024-10-17 17:55:35

还有 @tjholowaychuk(express 的创建者)的截屏视频,他使用了 @Vegar 描述的方法。

在 Vimeo 上提供:使用 Node.js 和 Express 的模块化 Web 应用程序

There also is a screencast of @tjholowaychuk (creator of express) where he uses the method @Vegar described.

Available on Vimeo: Modular web applications with Node.js and Express

顾铮苏瑾 2024-10-17 17:55:35

另一种选择;

App.js

var express = require('express')
      , routes = require('./routes')
      , user = require('./routes/user')
      , http = require('http')
      , path = require('path');

    var app = express();


// all environments
app.set('port', process.env.PORT || 3000);


app.get('/', routes.index);
app.get('/users/:id', user.getUser);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

index.js

exports.index = function(req, res){
  res.render('index', { title: 'Express' });
};

user.js

exports.getUser = function(req, res){


    //your code to get user

};

One more alternative;

App.js

var express = require('express')
      , routes = require('./routes')
      , user = require('./routes/user')
      , http = require('http')
      , path = require('path');

    var app = express();


// all environments
app.set('port', process.env.PORT || 3000);


app.get('/', routes.index);
app.get('/users/:id', user.getUser);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

index.js

exports.index = function(req, res){
  res.render('index', { title: 'Express' });
};

user.js

exports.getUser = function(req, res){


    //your code to get user

};
虫児飞 2024-10-17 17:55:35

查看有关express-routescan节点模块。该模块有助于为快速应用程序组织可维护的路由。你可以尝试一下。这对我来说是最好的解决方案。

Check out the article about the express-routescan node module. This module helps to organize maintainable routing for express application. You can try it. This is the best solution for me.

不如归去 2024-10-17 17:55:35

有几种方法可以做到:

1:

module1(app.route('/route1'));
module2(app.route('/route2'));

在模块中,您可以只实现 1 个函数来处理 http 方法:

module.exports = function(route) {
   route
   .get(function(req, res, next) {
       ...
   }).
   .post(function(req, res, next) {
      ...
   });
}

2:如果您想通过子应用程序而不是模块/中间件来处理路由:

var m1 = require(module1.js);
var m2 = require(module2.js);

app.use('/route1', r1);
app.use('/route2', r2);

There are several ways to do:

1:

module1(app.route('/route1'));
module2(app.route('/route2'));

In the modules you can just implement 1 function to handle the http methods:

module.exports = function(route) {
   route
   .get(function(req, res, next) {
       ...
   }).
   .post(function(req, res, next) {
      ...
   });
}

2: if you want to handle the route by a sub-app instead of a module/middleware :

var m1 = require(module1.js);
var m2 = require(module2.js);

app.use('/route1', r1);
app.use('/route2', r2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文