Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express call GET method within route from another route [duplicate]

I have multiple routes. How can I get the data from the user's route (GET method), by calling it within the GET method of the group's route? What is the best way of doing this?

My app.js looks like this:

var express = require('express');

var routes = require('./routes/index');
var users = require('./routes/users');
var groups = require('./routes/groups');

var app = express();

app.use('/', routes);
app.use('/users', users);
app.use('/groups', groups);

module.exports = app;
app.listen(3000);

Then I have another file routes/users.js:

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

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('GET ON THE users!');
});

module.exports = router;

And another route routes/groups.js:

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

/* GET groups listing. */
router.get('/', function(req, res, next) {

    // call the get on users and retrieve all data from that request

    res.send('GET for the groups');
});

module.exports = router;
like image 364
asuciu Avatar asked Aug 19 '16 20:08

asuciu


People also ask

What is a wildcard route Express?

Express allow you to use a wildcard within a route path using * . For example, the path defined below can be access using anything that follows the base URL (for example, if you wanted to build a “catch all” that caught routes not previously defined). app. get('/*',function(req,res) { req. send("I am Foo"); });

How do I nest a route in Express?

You can nest routers by attaching them as middleware on an other router, with or without params . You must pass {mergeParams: true} to the child router if you want to access the params from the parent router. Show activity on this post.

What does Express () method do?

Express provides methods to specify what function is called for a particular HTTP verb ( GET , POST , SET , etc.) and URL pattern ("Route"), and methods to specify what template ("view") engine is used, where template files are located, and what template to use to render a response.


2 Answers

You shouldn't use routing for that. Just call the function responsible for retrieving the users from the GET groups route and do what you need with that data. The way you propose is much more expensive because you will have to make a http call.

For simplicity I'm assuming that your logic is synchronous and data stored in data/users.js:

var data = [{id:1, name: "one"},{id: 2, name: "two"}];
module.exports = function(){
  return data;
};

in routes/users.js:

var express = require('express');
var router = express.Router();
var getUsers = required('./../data/users');

router.get('/', function(req, res, next) {
  res.send(getUsers());
});

in routes/groups.js:

var express = require('express');
var router = express.Router();
var otherRouter = require('./users')
var getUsers = require('./.../data/users');

router.get('/', function(req, res, next) {
  var users = getUsers();
  //do some logic to get groups based on users variable value
  res.send('GET for the groups');
});
like image 149
Slawomir Pasko Avatar answered Sep 17 '22 01:09

Slawomir Pasko


I consider what was being explained "forwarding", and it's quite useful, and available in other frameworks, in other languages.

Additionally, as a "forward" it does not have any overhead from a subsequent HTTP response.

In the case of Express, the following is available in version 4.X. Possibly other versions, but I have not checked.

var app = express()

function myRoute(req, res, next) {
  return res.send('ok')
}

function home(req, res, next) {
   req.url = '/some/other/path'

   // below is the code to handle the "forward".
   // if we want to change the method: req.method = 'POST'        
   return app._router.handle(req, res, next)
}

app.get('/some/other/path', myRoute)
app.get('/', home)
like image 36
conrad10781 Avatar answered Sep 21 '22 01:09

conrad10781