node.js 在 Express-js 中使用路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8864626/
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 15:01:07  来源:igfitidea点击:

Using routes in Express-js

node.jsurl-routingexpress

提问by Andreas Stokholm

So I'm starting to use Node.js. I saw the video with Ryan Dahl on Nodejs.organd heard he recommended Express-js for websites.

所以我开始使用 Node.js。我在Nodejs.org上看到了 Ryan Dahl 的视频,听说他为网站推荐了 Express-js。

I downloaded the latest version of Express, and began to code. I have a fully fledged static view up on /, but as soon as I try sending parameters, I get errors like this:

我下载了最新版本的 Express,并开始编写代码。我在 / 上有一个完全成熟的静态视图,但是一旦我尝试发送参数,就会收到如下错误:

Cannot GET /wiki

I tried following the guide on expressjs.combut the way one uses routes has changed in the latest version, which makes the guide unusable.

我尝试按照expressjs.com上的指南进行操作,但是在最新版本中使用路由的方式发生了变化,这使得该指南无法使用。

Guide:

指导:

app.get('/users/:id?', function(req, res, next){
    var id = req.params.id;
    if (id) {
        // do something
    } else {
        next();
    }
});

Generated by Express:

由 Express 生成:

app.get('/', routes.index);

My problem arises when I try and add another route.

当我尝试添加另一条路线时,我的问题出现了。

app.get('/wiki', routes.wiki_show);

I've tried a bunch of approaches, but I keep getting the Cannot GET /wiki(404) error.

我尝试了很多方法,但我不断收到Cannot GET /wiki(404) 错误。

routes/index.js looks like this:

route/index.js 看起来像这样:

exports.index = function(req, res) {
    res.render('index', { title: 'Test', articles: articles, current_article: current_article, sections: sections })
};

The only thing I did there was add some parameters (arrays in the same file) and this i working. But when I copy the contents and change exports.indexto exports.wikior exports.wiki_showI still get the Cannot GET /wikierror.

我在那里做的唯一一件事就是添加一些参数(同一文件中的数组),这就是我的工作。但是当我复制内容并更改exports.indexexports.wikior 时,exports.wiki_show我仍然收到Cannot GET /wiki错误消息。

Can anyone explain to me what I'm missing here? - Thanks.

谁能向我解释我在这里缺少什么?- 谢谢。

回答by Andreas Stokholm

So, after I created my question, I got this related list on the right with a similar issue: Organize routes in Node.js.

所以,在我创建我的问题之后,我在右边得到了一个类似问题的相关列表:在 Node.js 中组织路由

The answer in that post linked to the Express repo on GitHuband suggests to look at the 'route-separation' example.

该帖子中的答案链接到GitHub 上Express 存储库,并建议查看“路由分离”示例。

This helped me change my code, and I now have it working. - Thanks for your comments.

这帮助我更改了代码,现在我可以使用它了。- 感谢您的意见。

My implementation ended up looking like this;

我的实现最终看起来像这样;

I require my routes in the app.js:

我需要在 app.js 中使用我的路由:

var express = require('express')
  , site = require('./site')
  , wiki = require('./wiki');

And I add my routes like this:

我像这样添加我的路线:

app.get('/', site.index);
app.get('/wiki/:id', wiki.show);
app.get('/wiki/:id/edit', wiki.edit);

I have two files called wiki.js and site.js in the root of my app, containing this:

我的应用程序根目录中有两个名为 wiki.js 和 site.js 的文件,其中包含以下内容:

exports.edit = function(req, res) {

    var wiki_entry = req.params.id;

    res.render('wiki/edit', {
        title: 'Editing Wiki',
        wiki: wiki_entry
    })
}

回答by systemovich

The route-mapexpress example matches url paths with objects which in turn matches http verbs with functions. This lays the routing out in a tree, which is concise and easy to read. The apps's entities are also written as objects with the functions as enclosed methods.

路由映射快车例如匹配的URL与对象这反过来匹配HTTP动作与功能路径。这将路由布置在树中,简洁易读。应用程序的实体也被编写为对象,函数作为封闭的方法。

var express = require('../../lib/express')
  , verbose = process.env.NODE_ENV != 'test'
  , app = module.exports = express();

app.map = function(a, route){
  route = route || '';
  for (var key in a) {
    switch (typeof a[key]) {
      // { '/path': { ... }}
      case 'object':
        app.map(a[key], route + key);
        break;
      // get: function(){ ... }
      case 'function':
        if (verbose) console.log('%s %s', key, route);
        app[key](route, a[key]);
        break;
    }
  }
};

var users = {
  list: function(req, res){
    res.send('user list');
  },

  get: function(req, res){
    res.send('user ' + req.params.uid);
  },

  del: function(req, res){
    res.send('delete users');
  }
};

var pets = {
  list: function(req, res){
    res.send('user ' + req.params.uid + '\'s pets');
  },

  del: function(req, res){
    res.send('delete ' + req.params.uid + '\'s pet ' + req.params.pid);
  }
};

app.map({
  '/users': {
    get: users.list,
    del: users.del,
    '/:uid': {
      get: users.get,
      '/pets': {
        get: pets.list,
        '/:pid': {
          del: pets.del
        }
      }
    }
  }
});

app.listen(3000);

回答by Shlomi Loubaton

Seems that only index.js get loaded when you require("./routes") . I used the following code in index.js to load the rest of the routes:

似乎只有 index.js 在您 require("./routes") 时才会加载。我在 index.js 中使用了以下代码来加载其余的路由:

var fs = require('fs')
  , path = require('path');

fs.readdirSync(__dirname).forEach(function(file){
  var route_fname = __dirname + '/' + file;
  var route_name = path.basename(route_fname, '.js');
  if(route_name !== 'index' && route_name[0] !== "."){ 
    exports[route_name] = require(route_fname)[route_name];
  }
});

回答by Maleck13

You could also organise them into modules. So it would be something like.

您也可以将它们组织成模块。所以它会是这样的。

./
controllers
    index.js
    indexController.js
app.js

and then in the indexController.js of the controllers export your controllers.

然后在控制器的 indexController.js 中导出您的控制器。

//indexController.js
module.exports = function(){
//do some set up

var self = {
     indexAction : function (req,res){
       //do your thing
}
return self;
};

then in index.js of controllers dir

然后在控制器目录的 index.js 中

exports.indexController = require("./indexController");

and finally in app.js

最后在 app.js

var controllers = require("./controllers");

app.get("/",controllers.indexController().indexAction);

I think this approach allows for clearer seperation and also you can configure your controllers by passing perhaps a db connection in.

我认为这种方法可以实现更清晰的分离,并且您还可以通过传入数据库连接来配置控制器。

回答by Calvintwr

No one should ever have to keep writing app.use('/someRoute', require('someFile')) until it forms a heap of code.

没有人应该一直写 app.use('/someRoute', require('someFile')) 直到它形成一堆代码。

It just doesn't make sense at all to be spending time invoking/defining routings. Even if you do need custom control, it's probably only for some of the time, and for the most bit you want to be able to just create a standard file structure of routings and have a module do it automatically.

花时间调用/定义路由完全没有意义。即使您确实需要自定义控件,也可能只是在某些时候,并且在大多数情况下,您希望能够仅创建路由的标准文件结构并让模块自动执行此操作。

Try Route Magic

尝试路线魔法

As you scale your app, the routing invocations will start to form a giant heap of code that serves no purpose. You want to do just 2 lines of code to handle all the app.userouting invocations with Route Magic like this:

当你扩展你的应用程序时,路由调用将开始形成一大堆毫无用处的代码。你只想做 2 行代码来app.use使用 Route Magic处理所有路由调用,如下所示:

const magic = require('express-routemagic')
magic.use(app, __dirname, '[your route directory]')

For those you want to handle manually, just don't use pass the directory to Magic.

对于那些你想手动处理的,只是不要使用将目录传递给 Magic。