Organize routes in Node.js

前端 未结 6 1864
攒了一身酷
攒了一身酷 2020-12-04 04:57

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

相关标签:
6条回答
  • 2020-12-04 05:35

    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
    
    0 讨论(0)
  • 2020-12-04 05:38

    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
    
    };
    
    0 讨论(0)
  • 2020-12-04 05:39

    Check out the examples here:

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

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

    0 讨论(0)
  • 2020-12-04 05:42

    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.

    0 讨论(0)
  • 2020-12-04 05:45

    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

    0 讨论(0)
  • 2020-12-04 05:54

    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);
    
    0 讨论(0)
提交回复
热议问题