run-middleware

Express call GET method within route from another route

随声附和 提交于 2019-11-30 18:38:24
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()

Express call GET method within route from another route

戏子无情 提交于 2019-11-30 02:23:29
问题 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);

Calling already defined routes in other routes in Express NodeJS

為{幸葍}努か 提交于 2019-11-29 10:48:16
问题 I am writing a web app in node.js using Express. I have defined a route as follows: app.get("/firstService/:query", function(req,res){ //trivial example var html = "<html><body></body></html>"; res.end(html) }); How do I reuse that route from within express? app.get("/secondService/:query", function(req,res){ var data = app.call("/firstService/"+query); //do something with the data res.end(data); }); I couldn't find anything in the API documentation and would rather not use another library

Is it possible to call Express Router directly from code with a “fake” request?

半世苍凉 提交于 2019-11-28 06:55:50
Tangential to this question , I would like to find out if there is a way of triggering the Express Router without actually going through HTTP? The Router has a "private" method named handle that accepts a request, a response, and a callback. You can take a look at the tests that Express has for its Router . One example is: it('should support .use of other routers', function(done){ var router = new Router(); var another = new Router(); another.get('/bar', function(req, res){ res.end(); }); router.use('/foo', another); router.handle({ url: '/foo/bar', method: 'GET' }, { end: done }); }); The

Is it possible to call Express Router directly from code with a “fake” request?

大城市里の小女人 提交于 2019-11-27 05:43:35
问题 Tangential to this question, I would like to find out if there is a way of triggering the Express Router without actually going through HTTP? 回答1: The Router has a "private" method named handle that accepts a request, a response, and a callback. You can take a look at the tests that Express has for its Router. One example is: it('should support .use of other routers', function(done){ var router = new Router(); var another = new Router(); another.get('/bar', function(req, res){ res.end(); });