Tuesday 26 April 2016

Make request to other internal endpoint

Hi guys,TL;DR: I want to make a call to an endpoint on the same node.js-server.Basically, you can skip everything between the two lines. That's just some extra context.I'm working on a project in node.js that renders pages on the server-side (using EJS) for clients that are visiting through the browser. Soon, our web-application will also have a mobile-app on which pages are rendered on the client-side (so all I have to do is send json).So for all functionality, I will end up with two endpoints;(a) one endpoint that just responds with JSON.(b) one endpoint that responds with a rendered pageThe exact JSON generated in (a) can be used for rendering the page for (b). I want to implement the (b)-endpoint as follows: Make an internal api-call to endpoint (a). Use the result from this api-call (the json) to render the page. I know that this is probably not the best way to do it, but I would however like to know how it's done just for the sake of the argument.I have (b) implemented as follows: router.get('/getProblem', isAuthenticated, function (req, res) { https.request({ host: 'localhost', //eventually I would like to extract the host, port and path from the req.headers-object but for now I'm just trying to make this simple hardcoded request work. port: 3000, path: '/problem/req.query.problemId', method: 'GET', headers: req.headers },function(res) { console.log(res); //do stuff with result }); And (a) is implemented as follows:router.get('/problem/:id',isAuthenticated,function (req,res) { problemService.getProblemByProblemIdAndCompanyId(req.params.id, req.user.companyId).then(function (problem) { res.jsonp(problem); }) }); Calling http://localhost:3000/problem/1 works as expected and shows a json in my browser. However, calling http://localhost:3000/getProblem?problemId=39666 produces the following error on the server: GMT uncaughtException: socket hang upDoes anyone know how I would go about this? I also tried using the request-library without any luck.Kind regards,Duffman-

Submitted April 26, 2016 at 11:15PM by Duffman-

No comments:

Post a Comment