Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to add data to the response object in a middleware module in Express.js?

Tags:

Here's the basic setup. I'm trying to create a simple middleware component that would allow me to easily pass data from my route directly to my javascript in the client side. (Very similiar to the Gon gem in ruby). The way I'm doing it is by having a module that looks like this:

    module.exports = function(){     return function(req,res,next){         var app = req.app;         if(typeof(app) == 'undefined'){             var err = new Error("The JShare module requires express");             next(err);             return;         }         res.jshare = {};         app.dynamicHelpers({             includeJShare: function(req,res){                 if(typeof(res.jshare) === 'undefined'){                     return "";                 }                 return function(){                     return '<script type="text/javascript">window.jshare=' + JSON.stringify(res.jshare) + '</script>';                 }              }         });         next();     }; } 

Then, in my route I can do this:

exports.index = function(req, res){   res.jshare.person = {firstName : "Alex"};   res.render('index', { title: 'Express' }) }; 

Finally in the layout.jade:

!{includeJShare()} 

What that does is in outputs a line of javascript on the client that creates the exact JSON object that was created server side.

Here's the question; it all works as expected, but being new to Express and Node.js in general, I was just curious if attaching properties onto the response object is OK, or is there something wrong with doing it that I'm simply overlooking? For some reason it doesn't pass my "smell test" but I'm not sure why.....

like image 894
BFree Avatar asked Jul 05 '12 03:07

BFree


People also ask

Can middleware be used in response?

Middleware functions can perform the following tasks: Execute any code. Make changes to the request and the response objects. End the request-response cycle.

How many parameters you should pass in the middleware function?

Error-handling middleware always takes four arguments. You must provide four arguments to identify it as an error-handling middleware function.

Can we use middleware in response in node JS?

With Node. js middleware, you can run any code and modify the request and response objects. You can also call for the next middleware in the stack when the current one is completed. The example below will help you with the process of creating your Node.

What is the use of express JS response object?

The Response object (res) specifies the HTTP response which is sent by an Express app when it gets an HTTP request.


2 Answers

I know this is an old thread, but there is something else to add to this topic.

Express has a response.locals object which is meant for this purpose - extending the response from middleware to make it available to views.

You could add a property directly to the response object, and as @hasanyasin indicated, is how JavaScript is designed. But Express, more specifically, has a particular way they prefer we do it.

This may be new in express 3.x, not sure. Perhaps it didn't exist when this question was asked.

For details, see

http://expressjs.com/en/api.html#res.locals

There is also an app.locals for objects which don't vary from request to request (or response to response I suppose).

http://expressjs.com/en/api.html#app.locals

See also: req.locals vs. res.locals vs. res.data vs. req.data vs. app.locals in Express middleware

like image 147
Brandon Avatar answered Oct 09 '22 17:10

Brandon


It is perfectly OK. It is how JavaScript is designed. Only thing you should be careful is to not accidentally overriding already existing properties or being overridden by others. To be safer, instead of adding everything directly to req/res objects, you might consider going a level deeper:

res.mydata={} res.mydata.person= ... 

Like that.

like image 30
hasanyasin Avatar answered Oct 09 '22 17:10

hasanyasin