【问题标题】:Trouble organizing expressjs routes组织 expressjs 路线时遇到问题
【发布时间】:2012-12-03 00:32:49
【问题描述】:

我正在关注这个article,它描述了一种快速组织路线的好方法。但是,当我尝试访问从 main.js 文件中导出的函数时,我遇到了一个问题。 curl "localhost/user/username" 时出现 404 错误

//the routes section of my my app.js file
app.get('/', routes.index);
app.get('/user/:username', routes.getUser);

//my index.js file
require('./main');
require('./users');

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

//my main.js file

exports.getUser = function(req, res){
        console.log('this is getUser');
        res.end();
};

----用我的解决方案编辑----

这是我使用的解决方案,也许有人会觉得它有用。我也愿意听取有关这是否会在未来给我带来任何问题的建议。

//-------The routes in my app.js file now look like this.

require('./routes/index')(app);
require('./routes/main')(app);

//-------In index.js i now have this

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

//-------My main.js now looks like this-------

module.exports = function(app){

    app.get('/user/:username', function(req, res){
            var crawlUser = require('../engine/crawlUser');
            var username = req.params.username;
            crawlUser(username);
            res.end();
    });

};

【问题讨论】:

  • 您应该发布一个答案并接受您自己的答案。这样人们就会知道问题已经解决了,他们不需要花时间在上面。
  • 您的解决方案对我来说看起来不错。它看起来很像我在这里描述的:stackoverflow.com/questions/5778245/…

标签: node.js routes express


【解决方案1】:

全局变量是邪恶的,应该不惜一切代价避免。这是我在没有全局变量和没有过多样板代码的情况下组织路线的方法。

// File Structure

/app.js
/routes
/--index.js
/--main.js
/--users.js

// app.js
var app = require('express');

/* Call Middleware here */

require('routes')(app);
app.listen(3000);

---------------------------------------------

// routes/index.js - This is where I store all my route definitions
// in a long list organized by comments. Allows you to only need to go to
// one place to edit route definitions. 

module.exports = function(app) {

  var main = require('./main');
  app.get('/', main.get);

  var users = require('./users');
  app.get('/users/:param', users.get);

  ...

}

---------------------------------------------

// routes/main.js - Then in each submodule you define each function and attach
// to exports

exports.get = function(req, res, next){
  // Do stuff here
})

我想这最终是一个偏好问题,但是如果您希望您的代码保持敏捷性并与其他模块一起使用,您应该避免使用全局变量。即使亚历克斯·杨说没关系。 =)

【讨论】:

  • 感谢您抽出宝贵时间回答我的问题。如果您有兴趣,我决定采用一种不同的方法,我将其包含在我的原始帖子中。 @jibsales
  • 这与我建议的基本相同,唯一的区别是我选择只安装一个路由模块,因此我不必多次传递应用程序对象。当您有少量路线时,您的方式很好。我的项目中有超过 50 条路线,如果我不这样做,它就会变成一团糟。
【解决方案2】:

这是我使用的解决方案,也许有人会觉得它有用。

//-------The routes in my app.js file now look like this.

require('./routes/index')(app);
require('./routes/main')(app);

//-------In index.js i now have this

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

//-------My main.js now looks like this-------

module.exports = function(app){

    app.get('/user/:username', function(req, res){
            var crawlUser = require('../engine/crawlUser');
            var username = req.params.username;
            crawlUser(username);
            res.end();
    });

};

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 2014-11-15
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    相关资源
    最近更新 更多