首页 > 解决方案 > 如何使用 IP 地址从一个 Nodejs 应用程序向另一个应用程序发送请求?

问题描述

我有 2 个 Nodejs 服务器正在运行。其中一台服务器只有一个发布路线:

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

此服务器正在运行localhost:9000。如何从不同的 Nodejs 服务器触发 post 路由?

标签: javascriptnode.js

解决方案


您可以尝试类似的方法:

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);
    });
});

附加链接


推荐阅读