req.body 不会 return 回复
req.body will not return a response
我已经搜索并尝试了其他结果,但 none 似乎无法呈现任何结果。
我正在尝试使用 postman 将数据 post 发送到后端作为测试。响应是发送 'success' 消息,但每次我尝试时 req.body returns 都是一个空数组。我正在使用带有 express 的节点后端,并且我正在尝试使用路由。我以前有过成功,但由于某种原因这次我无法获得它,而且我的旧代码似乎不适用于这个。如果我只记录请求,而不是 req.body,我可以看到正文是空的,参数是空的,但是 url 包含参数。可以post,但是有点长,不知道有没有用
这是我要打的url:localhost:3000/?testParam=test&test=boop
app.js
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/blogTest', { useNewUrlParser: true });
mongoose.Promise = global.Promise;
const postsRoute = require('./routes/post');
app.use(postsRoute);
module.exports = app;
post.js
const express = require('express');
const router = express.Router();
const postModel = require('../models/post'); //not using this yet
router.get("/", function(req, res){
console.log("Getting Posts...");
postModel.find({}, function(err, posts){
if(err){
res.send("{'status': 'error', 'response': "+err+"}");
}
else if(posts.length < 1){
res.send("{'status': 'null', 'response': {}}");
}
else{
res.send("{'status': 'success', 'response': "+posts+"}");
}
});
});
router.post("/", function(req, res){
console.log(req.body);
res.send('success');
});
module.exports = router;
我期待控制台记录 {'testParam': 'test','test':'boop'}
另外,我尝试将 req.body 解析为 json 并将其字符串化,但它会导致服务器崩溃。
我认为您对 http 方法及其工作原理感到困惑。
req.body
是POST请求体,是客户端向你的api端点发送POST请求时传递的数据。如果您没有在 POST 请求中发送任何数据,则 req.body
将为空。
在您的示例中,要从 localhost:3000/?testParam=test&test=boop
之类的请求中获取数据,您需要查看 req.params
而不是 req.body
。
您需要尝试通过代码访问请求的 params 变量,尝试通过日志查找 params 可能不准确,因此请尝试
router.post("/", function(req, res){
console.log(req.params);
res.send('success');
});
我已经搜索并尝试了其他结果,但 none 似乎无法呈现任何结果。
我正在尝试使用 postman 将数据 post 发送到后端作为测试。响应是发送 'success' 消息,但每次我尝试时 req.body returns 都是一个空数组。我正在使用带有 express 的节点后端,并且我正在尝试使用路由。我以前有过成功,但由于某种原因这次我无法获得它,而且我的旧代码似乎不适用于这个。如果我只记录请求,而不是 req.body,我可以看到正文是空的,参数是空的,但是 url 包含参数。可以post,但是有点长,不知道有没有用
这是我要打的url:localhost:3000/?testParam=test&test=boop
app.js
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/blogTest', { useNewUrlParser: true });
mongoose.Promise = global.Promise;
const postsRoute = require('./routes/post');
app.use(postsRoute);
module.exports = app;
post.js
const express = require('express');
const router = express.Router();
const postModel = require('../models/post'); //not using this yet
router.get("/", function(req, res){
console.log("Getting Posts...");
postModel.find({}, function(err, posts){
if(err){
res.send("{'status': 'error', 'response': "+err+"}");
}
else if(posts.length < 1){
res.send("{'status': 'null', 'response': {}}");
}
else{
res.send("{'status': 'success', 'response': "+posts+"}");
}
});
});
router.post("/", function(req, res){
console.log(req.body);
res.send('success');
});
module.exports = router;
我期待控制台记录 {'testParam': 'test','test':'boop'}
另外,我尝试将 req.body 解析为 json 并将其字符串化,但它会导致服务器崩溃。
我认为您对 http 方法及其工作原理感到困惑。
req.body
是POST请求体,是客户端向你的api端点发送POST请求时传递的数据。如果您没有在 POST 请求中发送任何数据,则 req.body
将为空。
在您的示例中,要从 localhost:3000/?testParam=test&test=boop
之类的请求中获取数据,您需要查看 req.params
而不是 req.body
。
您需要尝试通过代码访问请求的 params 变量,尝试通过日志查找 params 可能不准确,因此请尝试
router.post("/", function(req, res){
console.log(req.params);
res.send('success');
});