BFree
BFree

Reputation: 103740

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

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.....

Upvotes: 24

Views: 29828

Answers (3)

Brandon
Brandon

Reputation: 10038

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

Upvotes: 45

JoeTidee
JoeTidee

Reputation: 26054

Use res.locals for including custom variables in your response object.

Upvotes: -2

hasanyasin
hasanyasin

Reputation: 6253

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.

Upvotes: 16

Related Questions