Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 240 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Express - Route separation methods

#1
I'm trying to find the best method to separate routes in Express. I'm aware of two methods and I was wondering what the difference is between them, and which one is considered 'best practice' (and why).

***Method one*** - exports

Lots of examples seem to use this method

app.js

var user = require('./routes/users');
app.get('/users', user.list);

routes/users.js

exports.list = function(req, res){
res.render('users', { title: 'Users', users: users });
};

***Method two*** -

This method is used in the express guide

app.js

var users = require('./routes/users');
app.use('/users', users);


routes/users.js

var express = require('express');
var router = express.Router();

router.get('/', function(req, res){
res.render('users', { title: 'Users', users: users });
});

module.exports = router;

So as I said above, which one is the preferred method, and why?
Reply

#2
***Method three*** - use both

You should be taking advantage of both the router introduced in Express 4 and the modularization of your controllers.

**app.js**

var routes= require('./routes');
app.use('/', routes);

**routes/index.js**

var express = require('express');
var users = require('../routes/users');
var router = express.Router();

router.use('/users', users);

module.exports = router;

**routes/users.js**

var express = require('express');
var users = require('../controllers/users');
var router = express.Router();

router.get('/', users.list);

module.exports = router;

**controllers/users.js**

exports.list = function(req, res){
res.render('users', { title: 'Users', users: users });
};
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through