你好,我是node js的新手,我今天才开始学习它,我只是想知道从HTML表单向node.js服务器发送数据是否可能,而不是让该服务器向另一个node.js服务器发送数据。
我已经解决了如何将HTML表单发送到第一台服务器,但是我能让第一台服务器将数据发送到第二台服务器吗?
索引html代码:
<form action="https://FirstSerer.com/example" method="POST">
firstname: <input type="text" name="firstname" /><br/>
lastname: <input type="text" name="lastname" /><br/>
<button type="submit">Send</button>
</form>
第一个服务器代码:
const express = require("express");
const bodyParser = require('body-parser');
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({
extended: true
}));
app.get("/", (request, response) => {
response.sendFile(__dirname + "/views/index.html");
});
app.post('/example', async (req, res) => {
try {
console.log(`Attempting to send firstname: ${req.body.firstname} and lastname: ${req.body.lastname}.`);
await res.send(`Attempting to send firstname: ${req.body.firstname} and lastname: ${req.body.lastname}.`);
// CAN I SEND THE DATA FROM HERE TO THE SECOND SERVER?
} catch (e) {
console.error("error", e);
}
});
const listener = app.listen(process.env.PORT, () => {
console.log("Your app is listening on port " + listener.address().port);
});
你能提供更多的细节吗?所以服务器是不同的域/站点? 我的意思是,你可以从节点代码向新服务器发送另一个post请求。。。我不知道这是否是你想要的。。。
否则,如果不需要任何验证或服务器端的任何东西,则只需在客户端向每个服务器发送ajax post请求,其中包含每个服务器的所有/仅需要的数据。 您还可以查看FormData对象,对于希望在客户端获取/编辑/删除某些值的更复杂的表单,该对象可能很有用