检索正文数据节点快递
Retrieve body data node express
我正在尝试检索正文数据,但我的终端只收到未定义的消息,不知道 how/where 是否可以检索“电子邮件”。我用谷歌搜索了一下,但找不到答案。
这是 app.js 文件:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
//routes which should handle request
app.post("/orders", (req, res, next) =>{
console.log(req.body.email);
res.json(["Orange", "Apple", "Banana"]);
});
//export app
module.exports = app;
这里是 server.js 文件:
const http = require('http');
//import app.js file
const app = require('./app');
//define port to be used
const port = process.env.PORT || 3100;
const server = http.createServer(app);
server.listen(port, () =>{
//print a message when the server runs successfully
console.log("Success connecting to server!");
});
我想接收“姓名”数据并在函数中使用它 return json。我正在使用 postman 仅通过一个名为“email”的键发送 post 请求。不过,Postman 收到了我编码的 Json 测试数据“Orange, Apple, Banana”。
对于 x-www-form-urlencoded
,您的示例应该可以正常工作(只需在 body 选项卡下的 Postman 中选择它)。
如果您想 POST 数据(例如文件)作为 multipart/form-data
您可以安装 multer middleware 并在您的应用中使用它:app.use(multer().array())
// File: app.js
const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer().array());
//routes which should handle request
app.post("/orders", async (req, res, next) =>{
console.log(req.body.email);
res.json(["Orange", "Apple", "Banana"]);
});
//export app
module.exports = app;
这适用于:
curl --location --request POST 'localhost:3100/orders' \
--form 'email=john@example.com'
和
curl --location --request POST 'localhost:3100/orders' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'email=john@example.com'
我正在尝试检索正文数据,但我的终端只收到未定义的消息,不知道 how/where 是否可以检索“电子邮件”。我用谷歌搜索了一下,但找不到答案。
这是 app.js 文件:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
//routes which should handle request
app.post("/orders", (req, res, next) =>{
console.log(req.body.email);
res.json(["Orange", "Apple", "Banana"]);
});
//export app
module.exports = app;
这里是 server.js 文件:
const http = require('http');
//import app.js file
const app = require('./app');
//define port to be used
const port = process.env.PORT || 3100;
const server = http.createServer(app);
server.listen(port, () =>{
//print a message when the server runs successfully
console.log("Success connecting to server!");
});
我想接收“姓名”数据并在函数中使用它 return json。我正在使用 postman 仅通过一个名为“email”的键发送 post 请求。不过,Postman 收到了我编码的 Json 测试数据“Orange, Apple, Banana”。
对于 x-www-form-urlencoded
,您的示例应该可以正常工作(只需在 body 选项卡下的 Postman 中选择它)。
如果您想 POST 数据(例如文件)作为 multipart/form-data
您可以安装 multer middleware 并在您的应用中使用它:app.use(multer().array())
// File: app.js
const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer().array());
//routes which should handle request
app.post("/orders", async (req, res, next) =>{
console.log(req.body.email);
res.json(["Orange", "Apple", "Banana"]);
});
//export app
module.exports = app;
这适用于:
curl --location --request POST 'localhost:3100/orders' \
--form 'email=john@example.com'
和
curl --location --request POST 'localhost:3100/orders' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'email=john@example.com'