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:
  • 663 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to export all routes in Express?

#1
I have an NodeJS Express app that is getting really big in just one file (app.js).

I want to export all my routes into a single, external file, say `./lib/routes.js`. How to do that?

How to export the following bit of code and require it correctly in main `app.js`?



app.get('/logout', function(req, res) {
res.render('logout', {
username: req.session.username
});
});

app.get('/dashboard', function(req, res) {
res.render('dashboard', {
username: req.session.username
});
});

app.get('/login', function(req, res) {
res.render('login', {
badLogin: false,
loginError: false
});
});
Reply

#2
Why not do something like this:

// logout.js
module.exports = function(req, res){
res.render('logout', {
username: req.session.username
});
});

// dashboard.js
module.exports = function(req, res){
res.render('dashboard', {
username: req.session.username
});
});

// login.js
module.exports = function(req, res){
res.render('login', {
badLogin: false,
loginError: false
});
});

// app.js
app.get('/logout', require('logout'));
app.get('/dashboard', require('dashboard'));
app.get('/login', require('login'));

Also, you could imagine easily using

[To see links please register here]

to loop through a *routes* directory and load these up programmatically.

You could even do something along the lines of...

module.exports.handler[] = {
method : 'get',
route : '/login',
action : res.render('login', {
badLogin: false,
loginError: false
});
}

Though I think I'd spend a little time thinking about how to simplify that.
Reply

#3
What I do is group my routes by controller. For each group of related routes (users, shopping cart, whatever), I make a controller file that lives in `app/controllers/foo.js` where `foo` is the controller name. In my main javascript server file (where all your code currently lives), I `require` each controller by name and then call its `setup` function, passing in my express `app` object, and allow the controller to add whatever routes it needs.

['api', 'authorization', 'users', 'tests'].map(function(controllerName) {
var controller = require('./controllers/' + controllerName);
controller.setup(app);
});

Inside each controller, I do something like:

exports.setup = function(app) {
app.get('/dashboard', function(req, res) {
res.render('dashboard', {
username: req.session.username
});
});
};
Reply

#4


using glob you can export all routes on directory for example '/routes':

> npm i --save glob

``` lang:js

// *** /routes/index.js file ***

const express = require('express')
const Router = express.Router
const router = Router()
const glob = require('glob')


/**
* options ignore files inside routes folder
*/
const options = {
ignore: [`${__dirname}/_helpers.js`, `${__dirname}/index.js`]
}

/**
* read all files on current directory and export routes as lowercase of the filename
* example 'routes/Products.js' route will be access by '/products'
*/
const routes =
glob.sync(__dirname + '/*.js', options)
.map(filename => {
const arr = filename.split('/')
let name = arr.pop();
name = name.replace('.js', '')
return {
path: `/${name.toLowerCase()}`,
router: require(`${filename.replace('.js', '')}`)
}
})
.filter(obj => Object.getPrototypeOf(obj.router) == Router)
.forEach(obj => router.use(obj.path, obj.router))


module.exports = router;
```


then

> on app.js

``` lang:js
// app.js file

const express = require('express')
const routes = require('./routes')


const app = express()

app.use('/api', routes)

```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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