0

Initially I tried to use the router.post and router.get methods and have them separated in my code.

Then I opted to use router.all, and within the same function separate POST and GET and use two res.render and a common object with the viewData they both share, as it will load the same screen.

I'm wondering if this is a good approach to it and how this is usually done in node.js / express.js:

router.all('/profile', function(req, res, next) {
    var viewData = {
        data1: Demo.getdata(1),
        data2: Demo.getdata(2),
        data3: Demo.getdata(3),
        data4: Demo.getdata(4)
    };

    if (req.method === 'POST') {
        request.get({
            uri: res.locals.baseUrl + '/getData',
            qs: {
                param1: param1
            }
        }, function(error, response, body) {
            if (error || response.statusCode != 200) {
                viewData.message = 'There was a problem!!';
            }
            else{
                var data = JSON.parse(body);
                viewData.message = 'Good job!!';
                viewData.moreData= data.moreData;
            }
            res.render('settings/profile', viewData);
        });

    }
    else{
        res.render('settings/profile', viewData);
    }
});
Alvaro
  • 40,778
  • 30
  • 164
  • 336
  • I suggest keeping it separate as I have mostly seen people explicitly define methods on a particular routes and define their callbacks or usecases. if you use `router.all` that particular route will be available for all the http methods. Using and if-else within that one callback will make your code dirty. – Rohail Najam Feb 20 '17 at 14:23
  • What would you do with the common data. A middleware ? A function? Isn't that also worse in terms of readability ? – Alvaro Feb 20 '17 at 15:17
  • You can use a middleware or function both which ever fits your case best. I personally use middlewares. – Rohail Najam Feb 21 '17 at 11:31
  • @RohailNajam so you suggest passing the data between the middleware the the `post` and `get` methods by using the `req` variable ? – Alvaro Feb 21 '17 at 11:37
  • yes you can place your data like this `req.viewData` and then receive it in your API callback or usercase. – Rohail Najam Feb 21 '17 at 13:05

1 Answers1

1

What I would suggest in your case is keeping them separated because they have nothing (almost) in common.

What I would do to avoid copy/pasting code is make router.post('/profile', ...) call res.redirect("/profile"); so that it would fallback to the GET when needed.

Telokis
  • 3,399
  • 14
  • 36
  • The POST modifies the data I pass to the view. (as seen in he request callback) As far as I know you can not pass data on a redirect unless you [do some kind of hack](http://stackoverflow.com/a/19038048/1081396) (which looks pretty ugly to me!) – Alvaro Feb 21 '17 at 11:34
  • Don't you want to save the data in any way ? – Telokis Feb 21 '17 at 12:35
  • Then, you could modify the data and pass it onto another middleware, maybe. And this middleware would do the `res.render`. Both routes (`GET` and `POST`) would use that middleware but `POST` would do some stuff before calling it. – Telokis Feb 21 '17 at 16:05