0

I have 2 Nodejs Server running. One of the server just has a post route:

app.post("/",(req,res)=>{
    console.log(`Someone sent a post request`)
})

This server is running on localhost:9000. How do I fire the post route from a different Nodejs Server?

jedu
  • 1,211
  • 2
  • 25
  • 57

1 Answers1

1

You could try something similar to this:

var request = require("request");

app.post("/", (req, res) => {
    var options = {
        method: 'POST',
        url: 'http://localhost:9000/employee',
        headers: { 'Content-Type': 'application/json' },
        body: { id: 1 },
        json: true
    };

    request(options, function (error, response, body) {
        if (error) throw new Error(error);

        console.log(body);
        // Process the body and return the response.
        return res.send(body);
    });
});

Additional link

Sagar Chilukuri
  • 1,430
  • 2
  • 17
  • 29
  • This works but can I use an IP address instead of localhost. For example going to route like 123.123.123:9000 ? – jedu Aug 16 '19 at 01:43
  • Yes. You can. But just make sure port `9000` has access (firewall settings) from where you are accessing. – Sagar Chilukuri Aug 16 '19 at 01:58