简体   繁体   English

如何使用IP地址从一个Node.js应用程序向另一个请求发送请求?

[英]How do I send a request from One Nodejs application to another using an IP address?

I have 2 Nodejs Server running. 我有2个Nodejs服务器正在运行。 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 . 该服务器在localhost:9000上运行。 How do I fire the post route from a different Nodejs Server? 如何从其他Nodejs服务器触发发布路由?

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 附加链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM