可以通过Axios和节点后端发送数据
我正在尝试访问从后端传递到前端的表达式,并通过Axios访问前端。
生产者方面:
const axios = require('axios');
const URL = "http://localhost:5000/"
let n1 = 3
let n2 = 5
let sum = n1+n2;
axios.post(URL, JSON.stringify({
expression: sum,
}))
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
在消费者方面:
const express = require('express')
const app = express()
app.post('/', (req, res) => {
console.log(req.body);
})
app.listen(5000, () => console.log())
最终,我想拥有消费者侧日志“ 8”
I am trying to access the expression passed from the backend to the frontend through axios.
Producer side:
const axios = require('axios');
const URL = "http://localhost:5000/"
let n1 = 3
let n2 = 5
let sum = n1+n2;
axios.post(URL, JSON.stringify({
expression: sum,
}))
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
and on the consumer side:
const express = require('express')
const app = express()
app.post('/', (req, res) => {
console.log(req.body);
})
app.listen(5000, () => console.log())
Ultimately, I would like to have the consumer side log "8"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无需转换为JSON,Axios本地服务JSON。
添加到服务器:
只需向您发送数据:
You don't need to convert to JSON, axios serves JSON natively.
Add to the server:
Just send you data:
来自“ express”文档:
在尝试注册端点之前,请尝试包括此内容:
您还可以使用
express.json()
并删除“身体偏好器”依赖项:From the "express" documentation:
Try including this before you try to register the endpoint:
You can also use
express.json()
and drop the 'body-parser' dependency:Axios也有一些安全措施,您应该将IP地址放入
const
url =“ http:// localhost:5000/”
Axios have some security measures as well, you should put your IP address in
const
URL = "http://localhost:5000/"