Node.js custom render building

198 views Asked by At

I'm building an API in node.js with express, and I would like to extend the basic res.send from ANY of the external route files before a response is sent, as to pre-format the response and append in additional data. How is this possible? Thanks in advance!

1

There are 1 answers

2
Dan Kohn On

That's the purpose of middleware. See http://expressjs.com/api.html#middleware

For example, these lines activate the CSRF middleware and then make the CSRF token available to templates and generate the CSRF cookie used by AngularJS:

.use(express.csrf())
.use(function (req, res, next) {
  res.cookie('XSRF-TOKEN', req.session._csrf);
  res.locals.csrftoken = req.session._csrf;
  next();
})