Reputation: 14054
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.
Upvotes: 0
Views: 3402
Reputation: 542
You need to add the router to the express app instance. This can be done using
app.use('/test', router);
The above line tells express
to route all requests to /test
to your router instance.
And inside your router, since you already specified the path, you should change the get
function to
router.get('/', function(req, res, next) {
request({
uri: 'http://my_virtual_machine:8080/rest/stuff/test/',
}).pipe(res)
});
Upvotes: 2