javascript - 如何使用 IP 地址从一个 Nodejs 应用程序向另一个应用程序发送请求?
问题描述
我有 2 个 Nodejs 服务器正在运行。其中一台服务器只有一个发布路线:
app.post("/",(req,res)=>{
console.log(`Someone sent a post request`)
})
此服务器正在运行localhost:9000
。如何从不同的 Nodejs 服务器触发 post 路由?
解决方案
您可以尝试类似的方法:
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);
});
});
附加链接
推荐阅读
- mqtt - Mqtt vernemq 创建流式传输
- c# - 参数不被接受,即使它是正确的类型
- java - 如何在雪花外部浏览器身份验证中自动插入用户名
- java - Object.equals(Boolean, Boolean) 与逻辑运算符 (Java)
- php - 当数据库没有问题时如何禁用单选按钮?
- perl - 如何在没有任何附加模块的情况下在迷你 Web 服务器中返回 json?
- php - 如何将多个数组映射或组合成一个数组?
- nginx - 带有 on_play PHP 身份验证的 Nginx RTMP
- java - 当我将枚举构造函数设为私有或默认时,它有什么区别?
- python - Python排序字典列表