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>
Related
this is api.js file
function apiKey(res, req, next) {
const api_key = '12345678';
console.log(req.query);
next();
}
module.exports = apiKey;
You named it wrong way. should be like this
function apiKey(req, res, next) {
const api_key = '12345678';
console.log(req.query);
next();
}
module.exports = apiKey;
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);
Trying to separate api and web route:
route/index.js
var api = function () {
router.get('/:id', function (req, res, next) {
...
});
return router;
}
var web = function () {
router.get('/:id', function (req, res, next) {
...
});
return router;
}
module.exports = {
api: api,
web: web
};
app.js
var indexAPI = require('./app/routes/accounts').api;
var indexWeb = require('./app/routes/accounts').web;
app.use('/api/index', indexAPI);
but it didn't route successfully.
I have change it to and it is working:
var api = (function () {
router.get('/:id', function (req, res, next) {
...
});
return router;
})();
var web = (function () {
router.get('/:id', function (req, res, next) {
...
});
return router;
})();
module.exports = {
api: api,
web: web
};
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]);
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