Monday, 5 June 2017

How to call another api via express?

My goal is to put http://localhost:3000/test into my browser and get the response from http://my_virtual_machine:8080/rest/stuff/test.

I followed this tutorial to setup Express, mongoose (although the final version of this will not connect to Mongo, only the VM's endpoints), etc. I produced a to-do list API, and it works as expected.

However, I can't figure out how to call my VM with this API.

I read "External API Calls With Express, Node.JS and Require Module", and came up with the following addition to server.js:

router = express.Router(),
request = require('request');

router.get("/test", function(req,res,next){
    request({
        uri: 'http://my_virtual_machine:8080/rest/stuff/test/',
    }).pipe(res)
})

But, when I put http://localhost:3000/test into my browser I get a 404 error in the console.

Here's a PLUNK of all my code. server.js contains the changes I have made to try to get this to work.

I'm an Express noob, so I'm not sure why this doesn't work.



via Travis Heeter

No comments:

Post a Comment