I need to create the following process. I have two endpoints and I need to pass to the second endpoint the result of the first endpoint in expressejs. I have thought to make something like the guide shows:
var cb0 = function (req, res, next) {
console.log('CB0');
next();
}
var cb1 = function (req, res, next) {
console.log('CB1');
next();
}
var cb2 = function (req, res) {
res.send('Hello from C!');
}
app.get('/example/c', [cb0, cb1, cb2]);
How should I pass to the second one function the result produced by the first one?
you need to create new property to req parameter like
var cb0 = function (req, res, next) {
// set data to be used in next middleware
req.forCB0 = "data you want to send";
console.log('CB0');
next();
}
var cb1 = function (req, res, next) {
// accessing data from cb0
var dataFromCB0 = req.forCB0
// set data to be used in next middleware
req.forCB1 = "data you want to send";
console.log('CB1');
next();
}
var cb2 = function (req, res) {
// accessing data from cb1
var dataFromCB2 = req.forCB1
// set data to be used in next middleware
req.forCB2 = "data you want to send";
res.send('Hello from C!');
}
app.get('/example/c', [cb0, cb1, cb2]);
Simple thing,
Just add result of one chain to request object, so you can access that object in another.
Here is your code.
var cb0 = function(req, res, next) {
req["CB0"] = "Result of CB0";
console.log('CB0');
next();
}
var cb1 = function(req, res, next) {
req["CB1"] = "Result of CB1";
console.log('CB1: Result of CB0: ' + req["CB0"]);
next();
}
var cb2 = function(req, res) {
console.log('CB2: Result of CB0: ' + req["CB0"]);
console.log('CB2: Result of CB1: ' + req["CB1"]);
res.send('Hello from C!');
}
app.get('/example/c', [cb0, cb1, cb2]);
Related
I have post.route.js where I mention
var post = require('../controllers/post.controller');
router.route('/posts').get(post.getPosts, post.setCache);
and my post.controller.js has the
exports.getPosts = function(req, res, next) {
var qstring = postQueryString;
getPostsDataByQuery(qstring,req,res,next);
}
function getPostsDataByQuery(queryString,req, res, next){
logger.info('start',req.route.path);
// some code here
return res.json(rows);
next();
};
exports.setCache = function(req, res, next){
console.log('here in set function');
cache.setExp(req, rows);
return true;
}
if in the setExp I log the not showing me
exports.setExp = function(req, data){
console.log('here');
}
You could make use of the next method:
function getPosts(queryString, req, res, next){
// your code
next();
};
function setCache(req, res) {
cache.setExp(req.originalUrl, process.env.CACHE_POST_EXP_TIME, rows);
return true;
}
router.route('/posts').get(post.getPosts, post.setCache);
I have the following sample middleware component that I want to display in the index.jade file. Is this possible? How do you add the req.requestTime in the Jade template?
var requestTime = function (req, res, next) {
req.requestTime = Date.now();
next();
};
app.use(requestTime);
Add a property to res.locals and you can use it in Pug (formerly Jade)
var requestTime = function (req, res, next) {
req.requestTime = Date.now();
res.locals.requestTime = req.requestTime;
next();
};
var requestTime = function (req, res, next) {
req.requestTime = Date.now();
res.locals.requestTime = req.requestTime;
next();
};
//Jade renderer
var jadeRenderer = function(req, res) {
var jadeFile = 'path/to/jade/template';
var jadeVars = {
locals: req.locals,
queryParams: req.query //if you desire?
};
res.render(jadeFile, jadeVars, function(err, html) {
if(err) {
//handle error
}
res.send(html);
});
};
//jade template
<p>#{locals.requestTime}</p>
Let say I want to add multiple arguments.
Here's the code
function firstArgument(req, res, next) {
// Do something
}
function secondArgument(req, res, next) {
// Do something
}
app.get('/something', firstArgument, secondArgument, function(req, res, next) {
// Is it possible to do this?
});
Is it possible? if so how does it works? Can anyone explain it to me.
Thank you
All the answers are in the express docs - http://expressjs.com/es/guide/routing.html
To summarize, for your scenario you can use:
var cb0 = function (req, res, next) {
console.log('CB0')
next()
}
var cb1 = function (req, res, next) {
console.log('CB1')
next()
}
app.get('/example/d', [cb0, cb1], function (req, res, next) {
console.log('response will be sent by the next function ...')
next()
}, function (req, res) {
res.send('Hello from D!')
})
or, without the second method.
var cb0 = function (req, res, next) {
console.log('CB0')
next()
}
var cb1 = function (req, res, next) {
console.log('CB1')
next()
}
app.get('/example/d', [cb0, cb1], function (req, res) {
res.send('Hello from D!')
})
Regarding how it works - it simply runs all the methods one after the other: when the next() method is called, the next method is being called.
When I use http://tes.com/routes, it will route to the api=>get('/'), instead of web=>get('/'). Why?
app.js:
var api = require('./app/routes/routes').api;
var transaction_web = require('./app/routes/routes').web;
app.use('/api/routes', transaction_api);
app.use('/routes', transaction_web);
routes.js:
var api = (function () {
router.get('/', function (req, res, next) {
...
});
return router;
})();
var web = (function () {
router.get('/', function (req, res, next) {
...
});
return router;
})();
module.exports = {
api: api,
web: web
};
The reason is because that's the order in which you're adding the routes.
This:
var api = (function () {
router.get('/', function (req, res, next) {
...
});
return router;
})();
is the same as:
router.get('/', function (req, res, next) {
...
});
var api = router;
The same thing happens with the other block where you assign web, so you end up with:
router.get('/', function (req, res, next) {
// api route
});
var api = router;
router.get('/', function (req, res, next) {
// web route
});
var web = router;
The solution would be to create separate Router instances. For example:
var api = new express.Router();
api.get('/', function (req, res, next) {
// web route
});
var web = new express.Router();
web.get('/', function (req, res, next) {
// web route
});
When I make GET http://localhost:8080/messages/3/sentiments for the code below why param method is called two times? So If I have 10 routes it will be called 10 times?
var comments = new Router();
comments.get('/comments', function (req, res, next) {
res.send('Comments by message_id=' + req.message._id);
})
var sentiments = new Router();
sentiments.get('/sentiments', function (req, res, next) {
res.send('Comments by message_id=' + req.message._id);
})
var messages = new Router();
messages.param('_message', function (req, res, next, _id) {
console.log("Set message");
fs.readFile(__filename, function () {
req.message = { _id: _id };
next();
});
})
messages.use('/messages/:_message/', comments);
messages.use('/messages/:_message/', sentiments);
app.use(messages);
app.listen(8080);
Confirmed to be a bug in Express < 4.3
https://github.com/visionmedia/express/issues/2121