The general way to approach refactoring is to determine what is different from your code, and extract it as the dynamic part that is passed to a function that contains the general code.
For example, two different things here are the way in which requests occur.
'/companies' vs '/companies/:id'
And the related path that is passed to http.get
'/companies' vs '/companies/' + req.params.id
You can extract them and pass them to a function that the handler will assign to you.
Here's a general approach:
// props contains the route and // a function that extracts the path from the request function setupGet(router, props) { router.get('/' + props.route, function(req, res, next) { http.get({ host: 'http://api.com', path: props.getPath(req) }, function(response) { var body = ''; response.on('data', function(d) { body += d; }); }); res.render('company', { data: body }); }); }
And then call it with two options:
setupGet(router, { route: 'companies', getPath: function(req) { return 'companies'; } }); setupGet(router, { route: 'companies/:id', getPath: function(req) { return 'companies' + req.params.id; } });
The advantage here is that you can use any combination of routes and paths, as well as use other req properties to determine the path.
Another thing you need to realize is that your res.render call will happen before you make body += d , because the former happens synchronously right after the http.get call, and the latter happens asynchronously (after some time).
You probably want to put the render method in the callback itself.
// props contains the route and // a function that extracts the path from the request function setupGet(router, props) { router.get('/' + props.route, function(req, res, next) { http.get({ host: 'http://api.com', path: props.getPath(req) }, function(response) { var body = ''; response.on('data', function(d) { body += d; // probably want to render here res.render('company', { data: body }); }); }); }); }