级联express framework服务器在node.js中获取调用
我是node.js和express的新手。我正在创建2台服务器,希望在从本地主机接收到服务器1上的get时调用get to server 2 第二台服务器从第一台服务器接收get后,会将响应传递回第一台服务器,然后第一台服务器会将获得的响应传递回本地主机 有什么最佳的方法可以做到这一点吗?有关于将express与node.js一起使用的教程可以参考吗级联express framework服务器在node.js中获取调用,node.js,express,udp,webstorm,Node.js,Express,Udp,Webstorm,我是node.js和express的新手。我正在创建2台服务器,希望在从本地主机接收到服务器1上的get时调用get to server 2 第二台服务器从第一台服务器接收get后,会将响应传递回第一台服务器,然后第一台服务器会将获得的响应传递回本地主机 有什么最佳的方法可以做到这一点吗?有关于将express与node.js一起使用的教程可以参考吗 谢谢。您最好的参考资料是。您所需的基本实现,用于从服务器1向服务器2发出HTTP请求: 服务器1: const express = require
谢谢。您最好的参考资料是。您所需的基本实现,用于从服务器1向服务器2发出HTTP请求: 服务器1:
const express = require('express');
const app = express();
const request = require('request');
app.get('/serverOne', (req, res, next) => {
request('http://localhost:3001/serverTwo', (err, response, body) => {
if (err) return next(err);
res.send(body);
});
})
app.listen(3000);
const express = require('express');
const app = express();
app.get('/serverTwo', (req, res) => {
res.send('hello world');
})
app.listen(3001);
HTTP/1.1 200 OK
Content-Type: text/html
hello world
服务器2:
const express = require('express');
const app = express();
const request = require('request');
app.get('/serverOne', (req, res, next) => {
request('http://localhost:3001/serverTwo', (err, response, body) => {
if (err) return next(err);
res.send(body);
});
})
app.listen(3000);
const express = require('express');
const app = express();
app.get('/serverTwo', (req, res) => {
res.send('hello world');
})
app.listen(3001);
HTTP/1.1 200 OK
Content-Type: text/html
hello world
请求:
const express = require('express');
const app = express();
const request = require('request');
app.get('/serverOne', (req, res, next) => {
request('http://localhost:3001/serverTwo', (err, response, body) => {
if (err) return next(err);
res.send(body);
});
})
app.listen(3000);
const express = require('express');
const app = express();
app.get('/serverTwo', (req, res) => {
res.send('hello world');
})
app.listen(3001);
HTTP/1.1 200 OK
Content-Type: text/html
hello world
GEThttp://localhost:3000/serverOne HTTP/1.1
响应:
const express = require('express');
const app = express();
const request = require('request');
app.get('/serverOne', (req, res, next) => {
request('http://localhost:3001/serverTwo', (err, response, body) => {
if (err) return next(err);
res.send(body);
});
})
app.listen(3000);
const express = require('express');
const app = express();
app.get('/serverTwo', (req, res) => {
res.send('hello world');
})
app.listen(3001);
HTTP/1.1 200 OK
Content-Type: text/html
hello world
你最好的推荐人是。您所需的基本实现,用于从服务器1向服务器2发出HTTP请求: 服务器1:
const express = require('express');
const app = express();
const request = require('request');
app.get('/serverOne', (req, res, next) => {
request('http://localhost:3001/serverTwo', (err, response, body) => {
if (err) return next(err);
res.send(body);
});
})
app.listen(3000);
const express = require('express');
const app = express();
app.get('/serverTwo', (req, res) => {
res.send('hello world');
})
app.listen(3001);
HTTP/1.1 200 OK
Content-Type: text/html
hello world
服务器2:
const express = require('express');
const app = express();
const request = require('request');
app.get('/serverOne', (req, res, next) => {
request('http://localhost:3001/serverTwo', (err, response, body) => {
if (err) return next(err);
res.send(body);
});
})
app.listen(3000);
const express = require('express');
const app = express();
app.get('/serverTwo', (req, res) => {
res.send('hello world');
})
app.listen(3001);
HTTP/1.1 200 OK
Content-Type: text/html
hello world
请求:
const express = require('express');
const app = express();
const request = require('request');
app.get('/serverOne', (req, res, next) => {
request('http://localhost:3001/serverTwo', (err, response, body) => {
if (err) return next(err);
res.send(body);
});
})
app.listen(3000);
const express = require('express');
const app = express();
app.get('/serverTwo', (req, res) => {
res.send('hello world');
})
app.listen(3001);
HTTP/1.1 200 OK
Content-Type: text/html
hello world
GEThttp://localhost:3000/serverOne HTTP/1.1
响应:
const express = require('express');
const app = express();
const request = require('request');
app.get('/serverOne', (req, res, next) => {
request('http://localhost:3001/serverTwo', (err, response, body) => {
if (err) return next(err);
res.send(body);
});
})
app.listen(3000);
const express = require('express');
const app = express();
app.get('/serverTwo', (req, res) => {
res.send('hello world');
})
app.listen(3001);
HTTP/1.1 200 OK
Content-Type: text/html
hello world
您将无法通过UDP执行此操作,因为HTTP仅为TCP。基本上,您需要在服务器1的路由处理程序中向服务器2发出请求,并将此请求的响应发送回客户端。我不是指通过UDP进行服务器通信。我删除了这一行以消除歧义。您将无法通过UDP执行此操作,因为HTTP仅为TCP。基本上,您需要在服务器1的路由处理程序中向服务器2发出请求,并将此请求的响应发送回客户端。我不是指通过UDP进行服务器通信。我删除了这一行以消除歧义。太好了。我现在可以在此基础上继续。PS:我还需要一个npm安装请求--save。因为这是我的第一个节点应用程序。谢谢cejast:)太好了。我现在可以继续了。PS:我还需要一个npm安装请求--save。因为这是我的第一个节点应用程序。谢谢cejast:)