在 Node.js 中组织路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4602212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:48:17  来源:igfitidea点击:

Organize routes in Node.js

node.jsexpressurl-routing

提问by NilColor

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:

我开始研究 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 );

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.

换句话说 - 我想要像 Django 的 URLConf 但在 Node.js 中的东西。

采纳答案by TK-421

Check out the examples here:

在此处查看示例:

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

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

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

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

回答by Vegar

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

我在 ' Smashing Node.js: JavaScript Everywhere' 中找到了一个我非常喜欢的简短示例。

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

通过定义module-amodule-b作为自己的快速应用程序,您可以使用connects app.use()将它们挂载到主应用程序中:

module-a.js

模块-a.js

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

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

  return app;
}();

module-b.js

模块-b.js

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

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

  return app;
}();

app.js

应用程序.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

回答by Koen.

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

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

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

Vimeo 上可用:带有 Node.js 和 Express 的模块化 Web 应用程序

回答by hadaytullah

One more alternative;

另一种选择;

App.js

应用程序.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

索引.js

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

user.js

用户.js

exports.getUser = function(req, res){


    //your code to get user

};

回答by Alexander Bykhov

Check out the articleabout the express-routescannode module. This module helps to organize maintainable routing for express application. You can try it. This is the best solution for me.

退房文章有关明示,routescan节点模块。该模块有助于为快速应用程序组织可维护的路由。你可以试试看。这对我来说是最好的解决方案。

回答by Minh Hoang

There are several ways to do:

有几种方法可以做到:

1:

1:

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

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

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

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 :

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

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

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